Cod sursa(job #1506530)

Utilizator Julian.FMI Caluian Iulian Julian. Data 20 octombrie 2015 19:18:49
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <iostream>
#include <fstream>
#define nmax 15007
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");

int c[nmax];

int main()
{long n,x,a,b,op,poz,p,k,i,j,sumb,suma;
    fin>>n>>k;


for(i=1;i<=n;i++)
{fin>>x;
poz=i;p=0;
        while(poz<=n)
        {c[poz]+=x;
         while(! ((poz>>p) & 1) )p++;
         poz+=(1<<p);
         p++;
        }


}

for(i=1;i<=k;i++)
{fin>>op>>a>>b;
if(op==0)
    {
        poz=a;p=0;
        while(poz<=n)
        {c[poz]-=b;
         while(! ((poz>>p) & 1) )p++;
         poz+=(1<<p);
         p++;
        }



    } else {

        poz=b; p=0; sumb=0;
        while(poz>0)
        {sumb+=c[poz];
        while(! ((poz>>p) & 1) )p++;
            poz-=(1<<p);
            p++;
        }

        poz=a-1;
        p=0;
        suma=0;
        while(poz>0)
        {suma+=c[poz];
        while(! ((poz>>p) & 1) )p++;
            poz-=(1<<p);
         p++;
        }
        fout<<sumb-suma<<'\n';

    }
}

}