Cod sursa(job #116390)

Utilizator radamiRadu Patulescu radami Data 18 decembrie 2007 15:44:37
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <stdio.h>

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


void add( long poz)
{
	if( unde > dr[poz] || unde < st[poz])
		return; // nu ne intersectam
	if ( st[poz] == dr[poz])
	{
		arbore[poz] += cat;
		return ;
	}
	
	add( poz *2);
	add( poz *2+1);
	/* 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) 
{
	if( dr[poz] < L || st[poz]  > R)//incluziune ciuciu
		return 0;
	if( L <= st[poz] && dr[poz] <= R)// incluziune totala
	{
		return arbore[poz];
	}
	
	return suma(poz*2) + suma(poz*2+1);
	
}
void creaza(long poz,long sst,long ddr)
{
	st[poz] = sst;
	dr[poz] = ddr;
	if( sst == ddr) return ;
	creaza( poz*2 ,sst,(sst+ddr)/2);
	creaza( poz*2+1 ,(sst+ddr)/2+1,ddr);
}


int main ()
{
	freopen("datorii.in","r",stdin);
	freopen("datorii.out","w",stdout);
	scanf("%d %d",&n,&m);
	
	creaza(1,1,n);
	for (int i = 1;i <= n;i ++)
	{
		int x;
		scanf("%d",&x);
		unde = i; cat = x;
		add(1);//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);
		}
		if( x == 1)
		{
			L = y;
			R = z;
			printf("%ld\n",suma(1));
		}
		
		
	}
	
	
	
	return 0;
}