Cod sursa(job #990884)

Utilizator BeilandArnoldArnold Beiland BeilandArnold Data 29 august 2013 10:14:17
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
int aib[15001];

int main(){
    std::ifstream fin("datorii.in");
    std::ofstream fout("datorii.out");

    int N,M;
    fin>>N>>M;

    int temp1,temp2,i,sum;
    char c;

    for(i=1;i<=N;++i){
        fin>>temp2;
        temp1=i;
        while(temp1<=N){
            aib[temp1]+=temp2;
            temp1 += temp1&(-temp1);
        }
    }

    while(M--){
        fin>>c>>temp1>>temp2;
        if(c=='0'){
            while(temp1<=N){
                aib[temp1]-=temp2;
                temp1 += temp1&(-temp1);
            }
        }
        else{
            sum=0; temp1--;
            while(temp1!=temp2)
                if(temp2>temp1){
                    sum+=aib[temp2];
                    temp2&=temp2-1;
                }
                else{
                    sum-=aib[temp1];
                    temp1-=temp1&(-temp1);
                }
            fout<<sum<<'\n';
        }
    }
}