Cod sursa(job #1505393)

Utilizator Iustin48Ventaniuc Iustin Iustin48 Data 19 octombrie 2015 09:04:56
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <fstream>
using namespace std;
ifstream f("datorii.in");
ofstream g("datorii.out");
int m,n,poz,sum,v[15001],val,t,c,a,b;
int main()
{
    f>>n>>m;
    for(int i=1;i<=n;++i)
    {
            f>>val;
            poz=i;
            c=0;
            while(poz<=n)
            {
                v[poz]+=val;
                while(!(poz&(1<<c))) ++c;
                poz+=(1<<c);
                c++;
            }
        }
    for(;m;--m)
    {
        f>>t;
        if(!t)
        {
            f>>poz>>val;
            c=0;
            while(poz<=n)
            {
                v[poz]-=val;
                while(!(poz&(1<<c))) ++c;
                poz+=(1<<c);
                c++;
            }
        }
        else
        {
            f>>a>>b;
            poz=b;
            c=0;
            sum=0;
            while(poz>0)
            {
                sum+=v[poz];
                while(!(poz&(1<<c))) ++c;
                poz-=(1<<c);
                c++;
            }
            poz=a-1;
            c=0;
            while(poz>0)
            {
                sum-=v[poz];
                while(!(poz&(1<<c))) ++c;
                poz-=(1<<c);
                c++;
            }
            g<<sum<<'\n';
        }
    }
    return 0;
}