Cod sursa(job #1365184)

Utilizator vlady1997Vlad Bucur vlady1997 Data 28 februarie 2015 10:13:10
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.72 kb
        #include <cstdio>
        #include <algorithm>
        using namespace std;
        int a[500001], Max, n;
        void update (int poz, int val, int nod, int st, int dr)
        {
            int m=(st+dr)/2;
            if (st==dr && st==poz)
            {
                a[nod]=val; return;
            }
            if (m<poz)
            {
                update(poz,val,nod*2+1,m+1,dr);
            }
            else if (m>=poz)
            {
                update(poz,val,nod*2,st,m);
            }
            a[nod]=max(a[nod*2],a[nod*2+1]);
        }
        void query(int x, int y, int nod, int st, int dr)
        {
            int m=(st+dr)/2;
            if (st>=x && dr<=y)
            {
                if (a[nod]>Max) Max=a[nod];
                return;
            }
            if (m>=x)
            {
                query(x,y,nod*2,st,m);
            }
            if (m<y)
            {
                query(x,y,nod*2+1,m+1,dr);
            }
        }
        int main()
        {
            int m, i, p, x, y;
            freopen("arbint.in","r",stdin);
            freopen("arbint.out","w",stdout);
            scanf("%d%d",&n,&m);
            for (i=1; i<=n; i++)
            {
                scanf("%d",&x);
                update(i,x,1,1,n);
            }
            for (i=1; i<=m; i++)
            {
                scanf("%d%d%d",&p,&x,&y);
                if (p==0)
                {
                    Max=-1;
                    query(x,y,1,1,n);
                    printf("%d\n",Max);
                }
                else
                {
                    update(x,y,1,1,n);
                }
            }
            return 0;
        }