Cod sursa(job #1197485)

Utilizator andreidei333andreidei andreidei333 Data 12 iunie 2014 10:26:43
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.64 kb
#include<cstdio>
#define ub(x) (x&(-x))
using namespace std;
int aib[15001];
int n;
void add(int x,int y){
    int i;
    for(i=x;i<=n;i+=ub(i))
        aib[i]+=y;
}
int sum(int x){
    int i,s=0;
    for(i=x;i>0;i-=ub(i))
        s+=aib[i];
    return s;
}
int main(){
    freopen ("datorii.in","r",stdin);
    freopen ("datorii.out","w",stdout);
    int m,i,p,x,y;
    scanf ("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        scanf ("%d",&x);
        add(i,x);
    }
    for(i=1;i<=m;i++){
        scanf ("%d%d%d",&p,&x,&y);
        if (p==0) add(x,-y);
        else printf ("%d\n",sum(y)-sum(x-1));
    }
    return 0;
}