Cod sursa(job #1524562)

Utilizator SilviuIIon Silviu SilviuI Data 14 noiembrie 2015 11:26:34
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <stdio.h>
#define nmax 100010
using namespace std;
int x,y,tip,i,n,m,arb[3*nmax];
inline int max(int a,int b) { if (a>b) return a; else return b; }
void build(int nod,int st,int dr)
{
    if (st==dr) { scanf("%d",&arb[nod]); return; } else
    {
        int m=(st+dr)/2;
        build(nod*2,st,m);
        build(nod*2+1,m+1,dr);
        arb[nod]=max(arb[nod*2],arb[nod*2+1]);
    }
}
int query(int nod,int st,int dr,int x,int y)
{
    if (st>=x && dr<=y) return arb[nod]; else
    {
        int m=(st+dr)/2,maxx=0;
        if (x<=m) maxx=max(maxx,query(nod*2,st,m,x,y));
        if (y>m) maxx=max(maxx,query(nod*2+1,m+1,dr,x,y));
        return maxx;
    }
}
void update(int nod,int st,int dr,int pos,int val)
{
    if (st==dr) { arb[nod]=val; return; } else
    {
        int m=(st+dr)/2;
        if (pos<=m) update(nod*2,st,m,pos,val); else
           if (pos>m) update(nod*2+1,m+1,dr,pos,val);
        arb[nod]=max(arb[nod*2],arb[nod*2+1]);
    }
}
int main() {
freopen("arbint.in","r",stdin);
freopen("arbint.out","w",stdout);
scanf("%d %d",&n,&m);
build(1,1,n);
for (i=1;i<=m;i++) {
    scanf("%d %d %d",&tip,&x,&y);
    if (tip==0) printf("%d\n",query(1,1,n,x,y)); else
        update(1,1,n,x,y);
}
return 0;
}