Cod sursa(job #1365477)

Utilizator vlady1997Vlad Bucur vlady1997 Data 28 februarie 2015 12:08:48
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.71 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)
            {
                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]=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)
            {
                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("datorii.in","r",stdin);
            freopen("datorii.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)
                {
                    update(x,y*(-1),1,1,n);
                }
                else
                {
                    Max=0;
                    query(x,y,1,1,n);
                    printf("%d\n",Max);
                }
            }
            return 0;
        }