Cod sursa(job #2564730)

Utilizator dragossofiaSofia Dragos dragossofia Data 2 martie 2020 09:49:36
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>
#define Nmax 100001
using namespace std;
ifstream fin ("aib.in");
ofstream fout("aib.out");
int n,m,AIB[Nmax],k;

void update( int poz , int val )
{
  while( poz <= n )
    {
      AIB[ poz ] += val ;
      poz += ( poz &- poz );
    }
}

int query ( int poz )
{
 int sum = 0 ;
 while ( poz )
    {
     sum += AIB[ poz ];
     poz -= ( poz &- poz );
    }
  return sum ;
}

void query2 ( int  k )
{
 int i , x  ;
 for ( i = 0 ; i <= n ; i ++ )
    {
     int x = query( i );
     if ( x == k  )
        {
         fout <<  i << "\n" ;
         return ;
        }
    }
}

void read( )
{
 fin>>n>>m;
 int x , task , a , b ;
 for( int i = 1 ; i <= n ; i ++ )
    {
     fin>>x;
     update( i , x );
    }
 for ( int  i = 1 ; i <= m ; i++ )
    {
     fin >>task ;
     if( task == 0 )
        {
         fin >> a >> b ;
         update( a , b );
        }
     if ( task == 1 )
        {
         fin >> a >> b ;
         fout << query ( b  ) - query ( a - 1 )<<"\n";
        }
     if (task == 2 )
        {
         fin >> k ;
         query2( k );
        }
    }


}

int main()
{
    read();
    return 0;
}