Cod sursa(job #1816358)

Utilizator ericutzdevilEric Spataru ericutzdevil Data 26 noiembrie 2016 13:16:15
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include<cstdio>

using namespace std;

int n,m,vi[10001],v[10001],i,j,x1,y1,x2,y2,nrElemBazaArbore,ans=0;

int maxim(int a,int b){
    if (a<=b)
        return b;
    return a;}

void update (int val, int poz, int st,int dr,int poz2){
    int mij;
    if (st==dr){
        v[poz2]-=val;
        return;}
    mij=(st+dr)/2;
    if (poz<=mij){
        update(val,poz,st,mij,2*poz2);}
    else{
        update(val,poz,mij+1,dr,2*poz2+1);}

    v[poz2]=v[2*poz2]+v[2*poz2+1];}

void query (int node, int left, int right, int a, int b){
    if (a<=left&&b>=right){
        ans=ans+v[node];
        return;}
    int mid=(left+right)/2;
    if (a<=mid)
        query (node*2,left, mid, a, b);
    if (b>mid)
        query (node*2+1,mid+1,right,a,b);}

int ceaMaiAprP2 (int n){
    int p=1,nrElem=0;
    while (n>p){
        p*=2;
        nrElem++;}
    return p;
}

void build(){
    int i,z=0,pozi;
    //baza
    for (i=ceaMaiAprP2(n);i<=ceaMaiAprP2(n)*2-1;i++){
        v[i]=vi[++z];}
    //restul
    pozi=ceaMaiAprP2(n)/2;
    while (pozi>=1){
        for (i=pozi;i<=pozi*2-1;i++){
            v[i]=v[2*i]+v[2*i+1];}
        pozi/=2;}}

int main()

{

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

int tip;

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

for (i=1;i<=n;i++)
    scanf ("%d",&vi[i]);

build();

for (i=1;i<=m;i++){
    scanf("%d%d%d",&tip,&x1,&x2);
    if (tip==1){
        query(1,1,ceaMaiAprP2(n),x1,x2);
        printf ("%d\n",ans);
        ans=0;
    }
    if (tip==0){
        update(x2,x1,1,ceaMaiAprP2(n),1);
    }

}

return 0;
}