Cod sursa(job #1376376)

Utilizator OnimushaLordTiberiu Copaciu OnimushaLord Data 5 martie 2015 17:13:20
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
# include <cstdio>
# include <algorithm>
# define N 250000

using namespace std;

int T[N];
int x,i,n,m;


void update(int nod, int st, int dr, int poz, int val)
{
    int m;
    if(dr<=poz && poz<=st)
    {
        T[nod]=val;
        return;
    }
    m=(st+dr)>>1;
    if(poz<=m) update(2*nod, st, m, poz, val);
    else update (2*nod+1, m+1, dr, poz, val);
    T[nod]=max(T[2*nod],T[2*nod+1]);
}

int query(int nod, int st, int dr, int a, int b)
{
    int x1=0,x2=0;
    if(a<=st && dr<=b) return T[nod];
    int m=(st+dr)>>1;
    if(a<=m) x1=query(2*nod, st, m, a, b);
    if(b>m) x2=query(2*nod+1, m+1, dr, a, b);
    return max(x1,x2);
}

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);

    scanf("%d %d\n", &n, &m);
    for(i=1; i<=n; ++i)
    {
        scanf("%d ", &x);
        update(1,1,n,i,x);
    }
    for(i=1; i<=m; ++i)
    {
        int op,x,y;
        scanf("%d %d %d\n", &op, &x, &y);
        if(!op) printf("%d\n", query(1,1,n,x,y));
        else update(1,1,n,x,y);
    }
}