Cod sursa(job #1153659)

Utilizator teo.serbanescuTeo Serbanescu teo.serbanescu Data 25 martie 2014 17:15:45
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>

using namespace std;

fstream f("datorii.in",ios::in);
fstream g("datorii.out",ios::out);

const int nmax=15005;

int n,m,cod,x,y,a[nmax],i;

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

long long suma(int ind)
{
    long long s=0;
    int poz=0;
    while (ind>0)
    {
        s+=a[ind];
        while (!(ind&(1<<poz))) poz++;
        ind-=1<<poz;
        poz++;
    }
    return s;
}

int main()
{
    f>>n>>m;
    for (i=1;i<=n;i++)
    {
        f>>x;
        update(i,x);
    }
    for (i=1;i<=m;i++)
    {
        f>>cod>>x>>y;
        if (cod==0)
        {
            update(x,-y);
        }
        if (cod==1)
        {
            g<<suma(y)-suma(x-1)<<'\n';
        }
    }
    f.close();g.close();
    return 0;
}