Cod sursa(job #2461167)

Utilizator Asgari_ArminArmin Asgari Asgari_Armin Data 24 septembrie 2019 22:47:14
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>

using namespace std;

ifstream fin( "aib.in" );
ofstream fout( "aib.out" );
const int NMAX = 100000;
int aib[NMAX + 1];
int n;

void update( int poz, int val ){
  int i;
  for( i = poz; i <= n; i += i&(-i) )
    aib[i] += val;
}

int query( int poz ){
  int i, sum;
  sum = 0;
  for( i = poz; i > 0; i -= i&(-i) )
    sum += aib[i];
  return sum;
}

int main() {
    int t, i, x, a, b;
    fin >> n >> t;
    for( i = 1; i <= n; ++i ){
      fin >> a;
      update(i, a);
    }
    for( i = 0; i < t; ++i ){
      fin >> x >> a;
      if( x == 1 || x == 0 )
        fin >> b;
      if( x == 0 )
        update( a, b );
      else if ( x == 1 )
        fout << query( b ) - query(a - 1) << "\n";
      else{
        int st, dr, med;
        st = 1;
        dr = n - 1;
        while( dr - st > 1 ){
          med = (st + dr) >> 1;
          if( query(med) > a )
            dr = med;
          else
            st = med;
        }
        fout << st << "\n";
      }
    }
    return 0;
}