Cod sursa(job #1688836)

Utilizator tudormaximTudor Maxim tudormaxim Data 13 aprilie 2016 19:19:14
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin ("arbint.in");
ofstream fout ("arbint.out");

const int nmax = 1e5+5;
int arb[nmax<<2], maxx;

void update(int nod, int st, int dr, int poz, int val) {
    if(st==dr) {
        arb[nod]=val;
        return;
    }
    int mij=(st+dr)>>1;
    if(poz<=mij) update(nod<<1, st, mij, poz, val);
    else update(nod<<1|1, mij+1, dr, poz, val);
    arb[nod]=max(arb[nod<<1], arb[nod<<1|1]);
}

void query(int nod, int st, int dr, int inc, int sf) {
    if(inc<=st && dr<=sf) {
        maxx=max(maxx, arb[nod]);
        return;
    }
    int mij=(st+dr)>>1;
    if(inc<=mij) query(nod<<1, st, mij, inc, sf);
    if(sf > mij) query(nod<<1|1, mij+1, dr, inc, sf);
}

int main() {
    ios_base::sync_with_stdio(false);
    int n, m, x, y, i, op;
    fin >> n >> m;
    for(i=1; i<=n; i++) {
        fin >> x;
        update(1, 1, n, i, x);
    }
    for(i=1; i<=m; i++) {
        fin >> op >> x >> y;
        if(op==0) {
            maxx=-1;
            query(1, 1, n, x, y);
            fout << maxx << '\n';
        }
        else update(1, 1, n, x, y);
    }
    fin.close();
    fout.close();
    return 0;
}