Cod sursa(job #385128)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 22 ianuarie 2010 08:47:14
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include<stdio.h>
#define N 15001
int a[65536],v[N],n,m,q,a1,b;
void build (int nod, int l,int r)
{
	if (l==r)
	{
		a[nod]=v[l];
		return;
	}
	int m=(l+r)>>1;
	build(2*nod,l,m);
	build(2*nod+1,m+1,r);
	a[nod]=a[2*nod]+a[2*nod+1];
}
void update(int nod, int l,int r,int poz, int val)
{
	if (l==r)
	{
		a[nod]=val;
		return;
	}
	int m=(l+r)>>1;
	if (poz<=m)
		update(2*nod,l,m,poz,val);
	else
	if (poz>m)
	update(2*nod+1,m+1,r,poz,val);
	a[nod]=a[2*nod]+a[2*nod+1];
}
int suma(int nod, int l, int r,int a1, int b)
{
	if (a1<=l&&r<=b)
		return a[nod];
	int m=(l+r)>>1,i1=0,i2=0;
	if (a1<=m)
		i1=suma(2*nod,l,m,a1,m);
	if (b>m)
		i2=suma(2*nod+1,m+1,r,m+1,b);
	return i1+i2;
}
void citire()
{
	freopen("datorii.in","r",stdin);
	freopen("datorii.out","w",stdout);
	scanf("%d%d",&n,&m);
	int i;
	for (i=1; i<=n; ++i)
	scanf("%d",&v[i]);
	build(1,1,n);
	while (m--)
	{
		scanf("%d%d%d",&q,&a1,&b);
		if (!q)
		{
			v[a1]=v[a1]-b;
			update(1,1,n,a1,v[a1]);
		}
		else
		{
			printf("%d\n",suma(1,1,n,a1,b));
		}
	}
}
int main()
{
	citire();
	return 0;
}