Cod sursa(job #834011)

Utilizator ephgstefana gal ephg Data 13 decembrie 2012 17:08:08
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include<algorithm>
using namespace std;

#define BM 100005
int n,m, a[BM*4+100];

inline void u(int nod,int st,int dr,int poz,int val){
    if(st==dr){
        a[nod]=val;
        return;
    }
    int m=(st+dr)/2;
    if(poz<=m)u(2*nod,st,m,poz,val);
    else u(2*nod+1,m+1,dr,poz,val);
    a[nod]=max(a[2*nod],a[2*nod+1]);
}
inline int q(int nod,int st,int dr,int aa,int b){
    int mx=0;
    if(st>=aa&&dr<=b)mx=max(mx,a[nod]);
    else {
        int m=(st+dr)/2;
        if(aa<=m)mx=max(mx, q(nod*2,st,m,aa,b));
        if(b>m)mx=max(mx, q(nod*2+1,m+1,dr,aa,b));
    }
    return mx;
}
int main(){
    int i,p=1,x,op,ls,ld;
    ifstream f ("arbint.in");
    ofstream g ("arbint.out");
    f >> n >> m;
    for(i=1;i<=n;++i){
        f >> x;
        u(1,1,n,i,x);
    }
    for(i=1;i<=m;++i){
        f >> op >> ls >> ld;
        if(op==1)u(1,1,n,ls,ld);
        else g << q(1,1,n,ls,ld) << '\n';
    }
    return 0;
}