Cod sursa(job #458179)

Utilizator crushackPopescu Silviu crushack Data 23 mai 2010 16:04:44
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <stdio.h>
#define lung 15000
struct arb
{
	int s,a,b;
	arb *st,*dr;
	arb()
	{
		s=a=b=0;
		st=dr=NULL;
	}
};

arb *A;
int a[lung];
void init(arb*,int,int,int);
void schimb(arb*,int,int);
int raport(arb*,int,int);

int main()
{
	int n,m,i,j,s,x,r1,r2;
	freopen("datorii.in","r",stdin);
	freopen("datorii.out","w",stdout);
	
	s=0;
	scanf("%d%d",&n,&m);
	for (i=0;i<n;i++)
		scanf("%d",&a[i]),s+=a[i];
	A=new arb;
	init(A,s,0,n-1);
	
	for (i=0;i<m;i++)
	{
		scanf("%d%d%d",&x,&r1,&r2);
		if (x)
		{
			r1--;r2--;
			printf("%d\n",raport(A,r1,r2));
		}
		else
		{
			r1--;
			schimb(A,r1,r2);
		}
	}
	return 0;
}

void init(arb *nod,int s,int x,int y)
{
	nod->s=s;
	nod->a=x;
	nod->b=y;
	if (x<y)
	{
		nod->st=new arb;
		init(nod->st,s-a[y],x,y-1);
		nod->dr=new arb;
		init(nod->dr,s-a[x],x+1,y);
	}
}

void schimb(arb *nod,int x,int val)
{
	if (x>=nod->a && x<=nod->b)
	{
		nod->s-=val;
		if (nod->st)
		{
			schimb(nod->st,x,val);
			schimb(nod->dr,x,val);
		}
	}
}

int raport(arb *nod,int x,int y)
{
	while (nod->a!=x || nod->b!=y)
	{
		if (nod->a < x)
			nod=nod->dr;
		if (nod->b > y)
			nod=nod->st;
	}
	return nod->s;
}