Cod sursa(job #1764192)

Utilizator CodrutLemeniCodrut Lemeni CodrutLemeni Data 25 septembrie 2016 09:39:12
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define N 15010

using namespace std;

int n;
int arb[N];
void update(int val,int nod){

    while(nod<=n){
        arb[nod]+=val;
        nod=nod+ ( nod ^ ( nod & (nod-1) ) );
    }

}

int query(int nod){
    static int s;

    s=0;

    while (nod>0){
        s+=arb[nod];
        nod = nod&(nod-1);
    }
    return s;
}

int main(){
    int i,m;
    int x,y,val,nod;
    int spy;

    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);

    scanf("%d%d",&n,&m);

    for(i=1;i<=n;i++){
        scanf("%d",&x);
        update(x,i);
    }

    for(i=0;i<m;i++){
        scanf("%d",&spy);
        if(spy==1){
            scanf("%d%d",&x,&y);
            y=query(y);
            x=query(x-1);
            printf("%d\n",y-x);
        }else{
            scanf("%d%d",&nod,&val);
            update(-val,nod);
        }

    }

    return 0;
}