Cod sursa(job #1153687)

Utilizator teo.serbanescuTeo Serbanescu teo.serbanescu Data 25 martie 2014 17:36:40
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <stdio.h>

using namespace std;

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

FILE *f,*g;

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=fopen("datorii.in","r");
    g=fopen("datorii.out","w");
    fscanf(f,"%d%d",&n,&m);
    for (i=1;i<=n;i++)
    {
        fscanf(f,"%d",&x);
        update(i,x);
    }
    for (i=1;i<=m;i++)
    {
        fscanf(f,"%d%d%d",&cod,&x,&y);
        if (cod==0)
        {
            update(x,-y);
        }
        if (cod==1)
        {
            fprintf(g,"%lld",suma(y)-suma(x-1));
            fprintf(g,"\n");
        }
    }
    fclose(f);fclose(g);
    return 0;
}