Cod sursa(job #392164)

Utilizator BaduBadu Badu Badu Data 6 februarie 2010 21:36:07
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include<fstream>
#define max 100001

using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");

int aib[max];
int n,m;

void UPDATE(int x, int val){
	
	for( ; x<= n; x += x & -x )
		aib[x] += val;
	
}

int SUM( int x ){
	
	int S=0;
	
	for( ; x ; x-= x & -x )
		S+=aib[x];
	
	return S;
}

void SEARCH( int s ){
	
	int i,step;
	
	for( step=1; step<n; step<<=1);
	
	for(i=0; step; step>>=1)
			if( i + step <= n && aib[i+step] <=s) { i+=step; s-=aib[i]; }
			
	if( s>0 && i<1 ) g<<"-1\n";
	else g<<i<<'\n';
}

int main(){
	
	f>>n>>m;
	int o,a,b,x;
	
	int i;
	for(i=1;i<=n;i++) { 
		f>>x;
		UPDATE( i, x );
	}
	
	for( ; m-- ; ){
		
		f>>o;
		if( o < 2) {
			f>>a>>b;
				
				if( !o ) UPDATE( a, b);
				else g<< SUM(b) - SUM(a-1)<<'\n';
				
				continue;
		}
		
		f>>a;
		
		SEARCH(a);
		
	}
	
	return 0;
}