Cod sursa(job #1266238)

Utilizator rzvrzvNicolescu Razvan rzvrzv Data 18 noiembrie 2014 15:13:09
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <cstdio>
#include <algorithm>

using namespace std;

int arb[1000002];
int n,m,i,poz,a,b,x,op;

void update(int nod,int st,int dr) {
    int mij;
    if (st>=poz && dr<=poz)
    {
        arb[nod]=x;
        return;
    }
    mij=(st+dr)>>1;
    if (poz<=mij)
        update(nod*2,st,mij);
    else
        update(nod*2+1,mij+1,dr);
    if (arb[nod*2]<arb[nod*2+1])
        arb[nod]=arb[nod*2+1];
    else
        arb[nod]=arb[nod*2];
}

int query(int nod, int st,int dr) {
    int mij,x1,x2;
    x1=x2=0;
    if (a<=st && dr<=b)
        return arb[nod];

    mij=(st+dr)>>1;
    if (a<=mij) x1=query(nod*2,st,mij);
    if (b>mij)  x2=query(nod*2+1,mij+1,dr);
    return max(x1,x2);
}

int main ()
{
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (i=1; i<=n; i++) {
        scanf("%d",&x);
        poz=i;
        update(1,1,n);
    }
    for (i=1; i<=m; i++) {
        scanf("%d%d%d",&op,&a,&b);
        if (op==0) {
            printf("%d\n",query(1,1,n));
        }
        else {
            poz=a;
            x=b;
            update(1,1,n);
        }
    }
    return 0;
}