Cod sursa(job #494535)

Utilizator ZethpixZethpix Zethpix Data 21 octombrie 2010 21:28:35
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <stdio.h>
#define LSB(x) ((x&(x-1))^(x))

int i,x,y,c,N,M,a[15002],sol;

void update(int pos,int val)
{
	while(pos<=N)
	{
		a[pos]+=val;
		pos+=LSB(pos);
	}
}

int sum(int pos)
{
	int s=0;
	while(pos>0)
	{
		s+=a[pos];
		pos-=LSB(pos);
	}
	return s;
}

int main()
{
	freopen("datorii.in","r",stdin);
	freopen("datorii.out","w",stdout);

	scanf("%d%d",&N,&M);
	for(i=1;i<=N;i++)
	{
		scanf("%d",&x);
		update(i,x);
	}

	for(i=1;i<=M;i++)
	{
		scanf("%d%d%d",&c,&x,&y);
		if(c==0) update(x,-y);
		else
		if(x>1) printf("%d\n",sum(y)-sum(x-1));
		else
		printf("%d\n",sum(y));
	}

	return 0;
}