Cod sursa(job #727896)

Utilizator tudgal1001Profir Tudor tudgal1001 Data 28 martie 2012 12:48:10
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include<cstdio>
using namespace std;

int n,a[15005];

void update (int poz, int val,int cst)
{
    while (poz<=n)
    {
        a[poz]=a[poz]+cst*val;
        poz+=(poz & (-poz));
    }
}

int query (int poz)
{
    int s=0;
    while (poz)
    {
        s+=a[poz];
        poz-=(poz & (-poz));
    }
    return s;
}

int main ()
{
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
    int m,i,x,y,tip;
    scanf("%d%d",&n,&m);
    for (i=1; i<=n; i++)
    {
        scanf("%d",&x);
        update(i,x,1);
    }
    for (i=1; i<=m; i++)
    {
        scanf("%d%d%d",&tip,&x,&y);
        if (!tip)
            update(x,y,-1);
        else printf("%d\n",query(y)-query(x-1));
    }
    return 0;
}