Cod sursa(job #1206780)

Utilizator acomAndrei Comaneci acom Data 11 iulie 2014 11:17:08
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,x,a,b,Max,A[400000];
void query(int nod, int st, int dr)
{
    if (a<=st && dr<=b)
        Max=max(Max,A[nod]);
    else
    {
        int mij=(st+dr)>>1;
        if (a<=mij)
            query(nod<<1,st,mij);
        if (mij<b)
            query((nod<<1)+1,mij+1,dr);
    }
}
void update(int nod, int st, int dr)
{
    if (st==dr) A[nod]=b;
    else
    {
        int mij=(st+dr)>>1;
        if (a<=mij)
            update(nod<<1,st,mij);
        else
            update((nod<<1)+1,mij+1,dr);
        A[nod]=max(A[nod<<1],A[(nod<<1)+1]);
    }
}
int main()
{
    int i;
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;++i)
    {
        scanf("%d",&x);
        a=i, b=x;
        update(1,1,n);
    }
    for (i=1;i<=m;++i)
    {
        scanf("%d%d%d",&x,&a,&b);
        if (x==0)
        {
            Max=0;
            query(1,1,n);
            printf("%d\n",Max);
        }
        else update(1,1,n);
    }
    return 0;
}