Cod sursa(job #1438338)

Utilizator andrei1998xAndrei Ionut andrei1998x Data 19 mai 2015 17:54:52
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include<fstream>
using namespace std;

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

int n, m, i, a[400010], v[100005], maxim, x, y, t;


void build(int nod, int st, int dr){
    //construieste arborele de int ce reprezinta vectorul initial
    if(st==dr)
        a[nod]=v[st];
    else{
        int mid=(st+dr)/2;
        build (2*nod, st, mid);
        build (2*nod+1, mid+1, dr);
        a[nod]=max(a[2*nod], a[2*nod+1]);
    }
}

void querry(int nod, int st, int dr, int x, int y){
    if(x<=st && y>=dr)
        maxim=max(maxim, a[nod]);
    else{
        int mid=(st+dr)/2;
        if(x<=mid)
            querry(2*nod, st, mid, x, y);
        if(y>mid)
            querry(2*nod+1, mid+1, dr, x, y);
    }
}
void update(int nod, int st, int dr, int x, int y){

    if(st==dr)
        a[nod]=y;
    else{
        int mid=(st+dr)/2;
        if(x<=mid)
            update(2*nod, st, mid, x, y);
        else
            update(2*nod+1, mid+1, dr, x, y);
        a[nod]=max(a[2*nod], a[2*nod+1]);
    }

}

int main(){
    fin>>n>>m;
    for(i=1; i<=n; i++)
        fin>>v[i];

    build(1, 1, n);


    for(i=1; i<=m; i++){
        fin>>t>>x>>y;
        if(t==0){
            maxim=0;
            querry(1, 1, n, x, y);
            fout<<maxim<<"\n";
        }else{
            update(1, 1, n, x, y); //elementul de pe pozitia x devine y
        }
    }


    return 0;
}