Cod sursa(job #1866713)

Utilizator EuAlexOtaku Hikikomori EuAlex Data 3 februarie 2017 14:29:12
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <cstdio>

using namespace std;

int aint[400090], ans;

int maxim(int a, int b) {
    if(a > b)
        return a;
    return b;
}

void update(int nod, int st, int dr, int poz, int a) {
    if(st == dr) {
        aint[nod] = a;
        return ;
    }
    int med;
    med = (st + dr) / 2;
    if(poz <= med) {
        update(2 * nod, st, med, poz, a);
    } else {
        update(2 * nod + 1, med + 1, dr, poz, a);
    }
    aint[nod] = maxim(aint[2 * nod], aint[2 * nod + 1]);
}

void query(int nod, int st, int dr, int x, int y) {
    if(x <= st && dr <= y) {
        ans = maxim(ans, aint[nod]);
        return ;
    }
    int med;
    med = (st + dr) / 2;
    if(x <= med) {
        query(2 * nod, st, med, x, y);
    }
    if(y >= med + 1) {
        query(2 * nod + 1, med + 1, dr, x, y);
    }
}

int main() {
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);

    int n, m, x, tip, a, b;
    scanf("%d%d", &n, &m);

    for(int i = 1; i <= n; ++ i) {
        scanf("%d", &x);
        update(1, 1, n, i, x);
    }

    for(int i = 1; i <= m; ++ i) {
        scanf("%d%d%d", &tip, &a, &b);
        if(tip == 0) {
            ans = 0;
            query(1, 1, n, a, b);
            printf("%d\n", ans);
        } else {
            update(1, 1, n, a, b);
        }
    }

    return 0;
}