Cod sursa(job #116380)

Utilizator radamiRadu Patulescu radami Data 18 decembrie 2007 15:35:08
Problema Datorii Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <stdio.h>

int n,m,arbore[50000];
int contor = 1;

/*int calc_putere(int k)
{
	if (k > 0)
		return 2*calc_putere(k-1);
	else
		return 1;
}*/


/*void build_arbore(int st,int dr,int niv,int aux)
{
	if (st  == dr)
		arbore[calc_putere(niv-1) + aux + nivel[niv]] = suma[nivel[niv]++];
	else
		if (st + 1 == dr)
			{
				arbore[calc_putere(niv) + nivel[niv] - 1] = suma[nivel[niv]++];
				arbore[calc_putere(niv) + nivel[niv] - 1] = suma[nivel[niv]++];
			}
		else
		{
			build_arbore(st,(st+dr)/2,niv+1,0);
			build_arbore((st+dr)/2+1,dr,niv+1,1);
		}

}*/

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


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);
		add(1,1,n,i,x);//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)
		{
			add(1,1,n,y,-z);
		}
		if( x == 1)
		{
			printf("%ld\n",suma(1,1,n,y,z));
		}
		
		
	}
	
	
	
	return 0;
}