Cod sursa(job #469570)

Utilizator dushmiMihai-Alexandru Dusmanu dushmi Data 8 iulie 2010 12:33:02
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include<cstdio>
#define N 1<<14
int n,m,aib[N];
int bit(int x)
{
    return (x&(x-1))^x;
}
void update(int poz,int x)
{
    while(poz<=n)
    {
        aib[poz]-=x;
        poz+=bit(poz);
    }
}
int suma(int poz)
{
    int s=0;
    while(poz)
    {
        s+=aib[poz];
        poz-=bit(poz);
    }
    return s;
}
int main()
{
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
    scanf("%d%d",&n,&m);
    int x,y,t;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        update(i,-x);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&t);
        if(t==0)
        {
            scanf("%d%d",&x,&y);
            update(x,y);
            continue;
        }
        scanf("%d%d",&x,&y);
        printf("%d\n",suma(y)-suma(x-1));
    }
    return 0;
}