Cod sursa(job #152904)

Utilizator thestickTudor A thestick Data 9 martie 2008 21:39:04
Problema Datorii Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <stdio.h>
#define logmax 14
#define LMAX 1<<(logmax+1)-1
#define zeros(x) (x^(x-1)&x)

int x[LMAX];
int n,m;

void init_aib()
{
int i;
for(i=0;i<LMAX;i++)x[i]=0;
}

void update(int p,int v)
{
int i;
for(i=p;i<LMAX;i+=zeros(i))
x[i]+=v;
}

int query(int p)
{
int i,rez=0;
for(i=p;i>0;i-=zeros(i))
rez+=x[i];
return rez;
}


void cit_rez()
{
FILE *f;
FILE *g;
int a,b,k,i;
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",&a);
update(i,a);
}
for(i=0;i<m;i++)
{
fscanf(f,"%d %d %d",&k,&a,&b);
if(k==0)
        {
        update(a,-b);
        }
if(k==1)
        {
        fprintf(g,"%d\n",query(b)-query(a-1));
        }
}
fclose(f);
fclose(g);
}

int main()
{
init_aib();
cit_rez();
return 0;
}