Cod sursa(job #934533)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 30 martie 2013 19:31:46
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <fstream>

#define MAX 100005
#define INF 0x3f3f3f3f
#define leftSon (nod << 1)
#define rightSon ((nod << 1) | 1)

using namespace std;

int N, M, aInt[(MAX << 2)];

void update(int nod, int L, int R, int poz, int val) {
    if(L == R) {
        aInt[nod] = val;
        return;
    } int M = (L + R) >> 1;
    if(poz <= M) update(leftSon, L, M, poz, val);
    else update(rightSon, M + 1, R, poz, val);
    aInt[nod] = max(aInt[leftSon], aInt[rightSon]);
}

int query(int nod, int L, int R, int Left, int Right) {
    if(Left <= L && R <= Right) return aInt[nod];
    if(L >= R) return INF;
    int M = (L + R) >> 1, ans = 0;
    if(Left <= M) ans = max(ans, query(leftSon, L, M, Left, Right));
    if(M < Right) ans = max(ans, query(rightSon, M + 1, R, Left, Right));
    return ans;
}

int main() {
    ifstream in("arbint.in"); ofstream out("arbint.out");
    in>>N>>M;
    for(int i = 1, A; i <= N; i++) {
        in>>A;
        update(1, 1, N, i, A);
    }
    for(int i = 1, A, B, C; i <= M; i++) {
        in>>A>>B>>C;
        if(A) update(1, 1, N, B, C);
        else out<<query(1, 1, N, B, C)<<"\n";
    } in.close(); out.close();
    return 0;
}