Cod sursa(job #518154)

Utilizator crushackPopescu Silviu crushack Data 30 decembrie 2010 17:03:17
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <stdio.h>
#define zeros(x) ( (x ^ (x - 1)) & x )
#define NMax 15005

const char IN[]="datorii.in",OUT[]="datorii.out";

int N,M,C,T,V,P,Q;
int a[NMax];
int arb[NMax];

inline void Add(int x,int quantity)
{
	for (; x<=N ; x+=zeros(x))
		arb[x] += quantity;
}

inline int Compute(int x)
{
	int ret=0;
	for (; x>0 ; x-=zeros(x))
		ret+= arb[x];
	return ret;
}

int main()
{
	int i;
	freopen(IN,"r",stdin);
	scanf("%d%d",&N,&M);
	for (i=1;i<=N;i++)
		scanf("%d",a+i),
		Add(i,a[i]);
	freopen(OUT,"w",stdout);
	for (i=0;i<M;i++)
	{
		scanf("%d",&C);
		switch(C)
		{
			case 0:
				scanf("%d%d",&T,&V);
				Add(T,-V);
			break;
			case 1:
				scanf("%d%d",&P,&Q);
				printf("%d\n", Compute(Q) - Compute(P-1));
			break;
		}
	}	
	fclose(stdout);
	fclose(stdin);
	return 0;
}