Cod sursa(job #773424)

Utilizator dutzulBodnariuc Dan Alexandru dutzul Data 1 august 2012 17:48:35
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#define LE 100606
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int n,m,i,xo,x,y,s,A[LE],tip;
void Update(int poz,int val){
	int ve;
	while (poz<=n)	
	{
	  A[poz]+=val;ve=1;
	  while (poz%(ve*2)==0) ve*=2;
	  poz=poz+ve;
	}
}
int Query1(int poz)
{
	int sum=0,ve;
	while (poz>0)
	{
	  sum+=A[poz];ve=1;
	  while (poz%(ve*2)==0) ve*=2;
	  poz=poz-ve;
	}
	return sum;
}
int Query2(int sum)
{
	int step,poz;
	for(step=1;step<n;step*=2);

	for(poz=0;step;step/=2)
	  if (poz+step<=n&&Query1(poz+step)<sum)
		  poz+=step;
	
	  poz+=1;
	  if (Query1(poz)!=sum) return -1; else return poz;
}
int main()
{
	f>>n>>m;
	for(i=1;i<=n;++i){ 
		f>>xo;
		Update(i,xo);
	}
	
	for(i=1;i<=m;++i)
	{
		f>>tip; 
		if (tip==0)
		   {
			   f>>x>>y; 
			   Update(x,y);			
		    }
		if (tip==1) 
			{
				f>>x>>y; 
				g<<Query1(y)-Query1(x-1)<<'\n';
		    }
	/*	if (tip==2) 
			{
				f>>s;    
			    g<<Query2(s)<<'\n';
			}*/
		}
	f.close();
	g.close();
	return 0;
}