Cod sursa(job #1147832)

Utilizator DanutsDanut Rusu Danuts Data 20 martie 2014 10:35:55
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include<stdio.h>
#include<algorithm>
#include<math.h>
#define maxn 500010
using namespace std;
FILE *f=fopen("datorii.in","r");
FILE *g=fopen("datorii.out","w");
int v[maxn],n,m,x,y,cod;
void opinit(int ind,int val){
    int poz=0;
    while(ind<=n){
        v[ind]=val;
        while(1<<poz==0)
            poz++;
        ind+=1<<poz;
        poz++;
    }
}
void op0(int ind,int val){
    int poz=0;
    while(ind<=n){
        v[ind]-=val;
      //  while(1<<poz==0)
        //    poz++;
       // ind+=1<<poz;
       // poz++;
       ind=n+1;
    }
}
int op1(int st,int dr){
    int s1=0,poz=0,s2=0;
    st--;
    while(st>0){
        s1+=v[st];
        while(1<<poz==0)
            poz++;
        st-=1<<poz;
        poz++;
    }
    poz=0;
    while(dr>0){
        s2+=v[dr];
        while(1>>poz==0)
            poz++;
        dr-=1<<poz;
        poz++;
    }
    return s2-s1;
}
int main (){
    fscanf(f,"%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        fscanf(f,"%d",&x);
        opinit(i,x);
    }
    for(int i=1;i<=m;i++){
            fscanf(f,"%d%d%d",&cod,&x,&y);
            if(cod==0){
                op0(x,y);

            }
            else
                fprintf(g,"%d\n",op1(x,y));
    }
    return 0;
}