Cod sursa(job #658349)

Utilizator thesilverhand13FII Florea Toma Eduard thesilverhand13 Data 8 ianuarie 2012 17:28:44
Problema Arbori indexati binar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
 # include <fstream>
 # include <vector>
 
 # define dim 100005
 
 using namespace std;
 
 ifstream f("aib.in");
 ofstream g("aib.out");
 
 int n, m;
 int arb[ dim ];
 
 inline int inc( int x )
 {
	 return ( x & -x );
 }
 
 inline void update( int poz, int x )
 {
	 int i;
	 for ( i = poz ; i <= n ; i = i + inc( i ) )
		 arb[ i ] = arb[ i ] + x ;
 }
 
 inline void cauta( int x )
 {
	 int i, j = 0, ok = 1;
	 for ( i = 1 ; i < n ; i <<= 1 );
	 
	 while ( i > 0 && ok  )
	 {
		 if ( j + i <= n )
		 {
			 if ( x >= arb[ i + j ] )
			 {
				 j = j + i;
				 x = x - arb[ j ];
				 if( x == 0 )
					 ok = 0;
			 }
		 }
		 i >>= 1;
	 }
	 if ( ok == 0 )
		 g << j << "\n";
	 else
		 g << -1 << "\n";
 }
 
 void det( int x, int y )
 {
	 int suma_a = 0, suma_b = 0, i;
	 
	 for ( i = y ; i >= 1 ; i = i - inc( i ) )
		 suma_a = suma_a + arb[ i ];
	 for ( i = x - 1 ; i >= 1 ; i = i - inc( i ) )
		 suma_b = suma_b + arb[ i ];
	 
	 g << suma_a - suma_b << "\n";
	 
 }
 
 inline void citire()
 {
	 int i, x, y, val;
	 f >> n >> m;
	 for ( i = 1 ; i <= n ; ++i )
	 {
		 f >> x;
		 update( i, x );
	 }
	 
	 for ( i = 1 ; i <= m ; i++ )
	 {
		 f >> val;
		 if ( val == 0 )
		 {
			 f >> x >> y;
			 update( x, y );
		 }
		// else
			 if ( val == 1 )
			 {
				 f >> x >> y;
				 det( x, y );
			 }
			 //else
				 if ( val == 2 )
				 {
					 f >> x;
					 cauta( x );
				 }
	 }
 }
 
 inline void afisare()
 {
	 int i;
	 for ( i = 1 ; i <= n ; ++i )
		 g << arb[ i ] << " ";
 }
 
 int main()
 {
	 citire();
	 //afisare();
	 return 0;
 }