Cod sursa(job #644645)

Utilizator chiar_nimeninimeni chiar_nimeni Data 7 decembrie 2011 11:18:38
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <cstdio>

int ab[50000],n,m,x,s,i,q,y;

void update(int nod, int st, int dr, int poz, int x)
{
    int m;
    m = (st+dr)/2;

    if ( st==dr ) ab[nod] += x;
    else if (poz>m)
    {
        update (2*nod+1, m+1, dr, poz, x);
        ab[nod] += x;
    }
    else
    {
        update (2*nod, st, m, poz, x);
        ab[nod] += x;
    }
}

void querry (int nod, int st, int dr, int a, int b)
{
    int m;
    if (a<=st && b>=dr) s += ab[nod];
    else
    {
        m = (st+dr)/2;
        if (a<=m) querry (2*nod, st, m, a, b);
        if (b>m) querry (2*nod+1, m+1, dr, a, b);
    }
}

int main()
{
    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 (1,1,n,i,x);
    }
    for (i=1; i<=m; i++)
    {
        scanf ("%d %d %d",&q,&x,&y);
        if ( q == 0 )
            update (1,1,n,x,-y);
        else
        {
            s = 0;
            querry (1,1,n,x,y);
            printf ("%d\n", s);
        }
    }
    return 0;
}