Cod sursa(job #1333978)

Utilizator rsteliRadu Stelian rsteli Data 3 februarie 2015 19:47:42
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
using namespace std;
ifstream cin("datorii.in");
ofstream cout("datorii.out");

#define nmax 15001

long long n,m,tip,x,y,a[nmax];

void update(int poz,int val)
{
    int c=0;
    while (poz<=n)
    {
        a[poz]+=val;
        while (!((1<<c)&poz))
            c++;
        poz+=1<<c;
        c++;
    }
}

int query(int poz)
{
   int c=0,s=0;
   while (poz)
   {
       s+=a[poz];
       while (!((1<<c)&poz))
              c++;
       poz-=1<<c;
       c++;
   }
   return s;
}

int main()
{
    int i,j;
    cin>>n>>m;
    for (i=1;i<=n;i++)
    {
        cin>>x;
        update(i,x);
    }
    for (i=1;i<=n;i++)
    {
        cin>>tip;
        if (!tip)
        {
            cin>>x>>y;
            update(x,-y);
        }
        else
        {
            cin>>x>>y;
            cout<<query(y)-query(x-1)<<'\n';
        }
    }
}