Cod sursa(job #972514)

Utilizator Master011Dragos Martac Master011 Data 11 iulie 2013 21:47:09
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include<cstdio>
#define CLOSE fclose(in); fclose(out); return 0;

const int lim = 15020;
int sum[lim],C[lim],cod[lim];
FILE *in,*out;

int search_bin(int x){
    int R=0, pas=1<<5,cc=5;
    while(pas){
        if(x%pas==0){
            x/=pas;
            R+=cc;
        }
        cc--;
        pas>>=1;
    }
    return R;
}

void modif(int Z, int V, int n){
    while (Z<=n){
        C[Z]-=V;
        Z+=(1<<cod[Z]);
    }
}

void rasp(int st, int dr){
    int S1=0,S2=0;
    while (st){
        S1+=C[st];
        st-=(1<<cod[st]);
    }
    while (dr){
        S2+=C[dr];
        dr-=(1<<cod[dr]);
    }
    fprintf(out,"%d\n",S2-S1);
}

int main(){
    in=fopen("datorii.in","r");
    out=fopen("datorii.out","w");
    int n,m,q;
    fscanf(in,"%d%d",&n,&m);
    for(int i = 1 ; i <= n ; ++i){
        fscanf(in,"%d",&q);
        sum[i]=sum[i-1]+q;
    }

    int Nr0,st,dr;
    for(int i = 1 ; i <= n ; ++i){
        Nr0=search_bin(i);
        cod[i]=Nr0;
        st=i-(1<<Nr0)+1;
        dr=i;
        C[i]=sum[dr]-sum[st-1];             //0,T,V
    }
    m++;
    int val,a,b;
    while(--m){
        fscanf(in,"%d%d%d",&val,&a,&b);
        if(!val)
            modif(a,b,n);
        else
            rasp(a-1,b);

    }
    return 0;
}