Cod sursa(job #287521)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 24 martie 2009 22:16:45
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include<stdio.h>
long n,m;
long v[20010];
long aib[20010];

long query(long x)
{
	long s=0;
	int i=0;
	while(x>0)
	{
		s=s+aib[x];
		for(;i<=31;i++)
			if(x&(1<<i))
			{
				x=x-(1<<i);
				break;
			}
	}
	return s;
}

void update(long x,long s)
{
	int i=0;
	while(x<=n)
	{
		aib[x]-=s;
		for(;i<=31;i++)
			if(x&(1<<i))
			{
				x=x+(1<<i);
				break;
			}
	}
}

void update_init(long x, long s)
{
	int i=0;
	while(x<=n)
	{
		aib[x]+=s;
		for(;i<=31;i++)
			if(x&(1<<i))
			{
				x=x+(1<<i);
				break;
			}
	}
}

void read()
{
	freopen("datorii.in","r",stdin);
	freopen("datorii.out","w",stdout);
	scanf("%ld%ld",&n,&m);
	long i;
	int tip;
	long a,b;
	for(i=1;i<=n;i++)
	{
		scanf("%ld",&v[i]);
		update_init(i,v[i]);
	}
	for(i=1;i<=m;i++)
	{
		scanf("%d",&tip);
		scanf("%ld%ld",&a,&b);
		if(tip==0)
		{
			update(a,b);//ind a cu suma b
		}
		else
		{
			printf("%ld\n",query(b)-query(a-1));
		}
	}
}

int main()
{
	read();
	return 0;
}