Cod sursa(job #1295498)

Utilizator TibixbAndrei Tiberiu Tibixb Data 19 decembrie 2014 17:28:01
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include<fstream>
using namespace std;
int A[400003], v[400003], n, m, i, sol, tip, a, b;
ifstream in("arbint.in");
ofstream out("arbint.out");
void build(int st, int dr, int nod){
    if(st==dr)
        A[nod]=v[st];
    else{
        int mij=st+(dr-st)/2;
        build(st, mij, 2*nod);
        build(mij+1, dr, 2*nod+1);
        A[nod]=max(A[2*nod], A[2*nod+1]);
    }
}
void update(int st, int dr, int nod, int poz, int val){
    if(st==dr)
        A[nod]=val;
    else{
        int mij=st+(dr-st)/2;
        if(poz<=mij)
            update(st, mij, 2*nod, poz, val);
        if(poz>=mij+1)
            update(mij+1, dr, 2*nod+1, poz, val);
        A[nod]=max(A[2*nod], A[2*nod+1]);
    }
}
void query(int st, int dr, int nod, int a, int b){
    if(a<=st && dr<=b)
        sol=max(sol, A[nod]);
    else{
        int mij=st+(dr-st)/2;
        if(a<=mij)
            query(st, mij, 2*nod, a, b);
        if(b>=mij+1)
            query(mij+1, dr, 2*nod+1, a, b);
    }
}
int main(){
    in>>n>>m;
    for(i=1; i<=n; i++)
        in>>v[i];
    build(1, n, 1);
    for(i=1; i<=m; i++){
        in>>tip>>a>>b;
        if(tip==1)
            update(1, n, 1, a, b);
        else{
            sol=-1;
            query(1, n, 1, a, b);
            out<<sol<<"\n";
        }
    }
return 0;
}