Cod sursa(job #1012412)

Utilizator hevelebalazshevele balazs hevelebalazs Data 18 octombrie 2013 21:58:15
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <stdio.h>
#define N1 45000
#define fr(i,a,b) for(int i=a;i<b;++i)
int a[N1];
int n,m;
void update(int l,int r,int p,int i,int j){
    int c=(l+r)/2;
    a[p]+=j;
    if(l>=r) return;
    if(c<i) update(c+1,r,2*p+1,i,j);
    else update(l,c,2*p,i,j);
    }
int query(int l,int r,int p,int i,int j){
    if(j<l||i>r) return 0;
    if(l==r) return a[p];
    if(l==i&&r==j) return a[p];
    int c=(l+r)/2;
    return query(l,c,2*p,i,c)+query(c+1,r,2*p+1,c+1,j);
    }
int main(){
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
    scanf("%i%i",&n,&m);
    int a,b,c;
    fr(i,0,n) scanf("%i",&a),update(0,n-1,1,i,a);
    fr(i,0,m){
        scanf("%i%i%i",&a,&b,&c);
        if(a==0) update(0,n-1,1,b-1,-c);
        if(a==1) printf("%i\n",query(0,n-1,1,b-1,c-1));
        }
    return 0;
    }