Cod sursa(job #2082648)

Utilizator LucaSeriSeritan Luca LucaSeri Data 6 decembrie 2017 17:25:51
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

int n;
const int MAXN = 1e5 + 10;
int arb[MAXN<<1];

void build(){
    for(int i = n; i > 0; --i){
        arb[i] = max(arb[i<<1], arb[i<<1|1]);
    }
}
void update(int poz, int val){
    poz += n;
    arb[poz] = val;
    while(poz > 1){
        arb[poz>>1] = max(arb[poz], arb[poz^1]);
        poz>>=1;
    }
}

int query(int st, int dr){
    int best = -1;
    st += n;
    dr += n;
    while(st < dr){
        if(st&1) best = max(best, arb[st++]);
        if(dr&1) best = max(best, arb[--dr]);
        st>>=1;
        dr>>=1;
    }

    return best;
}
int main(){
    ifstream f("arbint.in");
    ofstream g("arbint.out");
    int m;
    f >> n >> m;

    for(int i = 1; i <= n; ++i){
        int x;
        f >> arb[i+n];
    }
    build();
    for(int i = 1; i <= m; ++i){
        int cd, x, y;
        f >> cd >> x >> y;
        if(cd == 0) g << query(x, y+1) <<'\n';
        else update(x, y);
    }

    return 0;
}