Cod sursa(job #116385)

Utilizator radamiRadu Patulescu radami Data 18 decembrie 2007 15:38:36
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <stdio.h>

int n,m,arbore[50000],L,R;
int contor = 1,unde ,cat;



void add( long poz,long st,long dr)
{
	if( unde > dr || unde < st)
		return; // nu ne intersectam
	if ( st == dr)
	{
		arbore[poz] += cat;
		return ;
	}
	
	add( poz *2, st , (st+dr)/2);
	add( poz *2+1, (st + dr)/2+1,dr);
	/* pentru maxim
	arbore[poz] = max( arbore[poz*2],arbore[poz*2+1]);
	*/
	arbore[poz] += cat;
	/* pentru suma
	arbore[poz] = sum(........)
	arbore[poz] += cat; // oricare din ele
	*/
}
// poz = pozitia in arbore,  st,dr limitele intervalului poz, L  R limitele de insumare
long suma( long poz,long st,long dr) 
{
	if( dr < L || st  > R)//incluziune ciuciu
		return 0;
	if( L <= st && dr <= R)// incluziune totala
	{
		return arbore[poz];
	}
	
	return suma(poz*2, st  , (st+dr)/2 ) + suma( poz*2+1 , (st+dr)/2+1, dr);
	
}


int main ()
{
	freopen("datorii.in","r",stdin);
	freopen("datorii.out","w",stdout);
	scanf("%d %d",&n,&m);
	
	for (int i = 1;i <= n;i ++)
	{
		int x;
		scanf("%d",&x);
		unde = i; cat = x;
		add(1,1,n);//1 pozitia in arbore ; 1 -n intervalul; i pozitia modificata; noua valoare
	}
	
	
	//build_arbore(1,n,1,0);

	for (int i = 1;i <= m; i++)
	{
		long x,y,z;
		
		scanf("%ld %ld %ld",&x,&y,&z);
		if( x == 0)
		{
			unde = y;
			cat = -z;
			add(1,1,n);
		}
		if( x == 1)
		{
			L = y;
			R = z;
			printf("%ld\n",suma(1,1,n));
		}
		
		
	}
	
	
	
	return 0;
}