Cod sursa(job #644671)

Utilizator chiar_nimeninimeni chiar_nimeni Data 7 decembrie 2011 12:03:24
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <cstdio>

int ab[50000],n,m,x,s,i,q,y,t;
char l[5000],ch;

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",&n,&m);
	i=0;
	t=0;
    fgets (l, 5000, stdin);
    while (i<n)
    {
        if (l[t]>='0' && l[t]<='9')
        {
            x *=10;
            x += l[t] - '0';
        }
        else
        {
            update (1,1,n,i+1,x);
            i++;
            x=0;
        }
		t++;
    }
    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;
}