Cod sursa(job #1247772)

Utilizator raduchirilaChirila Radu Razvan raduchirila Data 23 octombrie 2014 19:05:49
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include<cstdio>
#define MAXN 15005
using namespace std;
int v[MAXN],n,m,aib[MAXN];
int query(int poz)
{
    int s=0;
    for(;poz;poz-=poz&(poz-1)^poz)
        s+=aib[poz];
    return s;
}
void update(int poz,int val)
{
    for( ; poz<=n; poz+=poz&(poz-1)^poz )
        aib[poz]+=val;
}
int main()
{
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
    int cod;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&cod);
        update(i,cod);
    }
    for(int i=1;i<=m;++i)
    {
        int x,y;
        scanf("%d%d%d",&cod,&x,&y);
        switch(cod)
        {
            case 0:
            update(x,(-1)*y);break;
            case 1:
            printf("%d\n",query(y)-query(x-1));break;
        }
    }
    return 0;
}