Cod sursa(job #998913)

Utilizator assa98Andrei Stanciu assa98 Data 18 septembrie 2013 19:01:34
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <cstdio>
using namespace std;

const int MAX_N=15100;

int v[MAX_N];
int n;

void update(int poz,int val) {
    while(poz<=n) {
        v[poz]-=val;
        poz+=(poz&(poz-1))^poz;
    }
}

int query(int poz) {
    int ans=0;
    while(poz) {
        ans+=v[poz];
        poz-=(poz&(poz-1))^poz;
    }
    return ans;
}

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