Cod sursa(job #1355469)

Utilizator lucian666Vasilut Lucian lucian666 Data 22 februarie 2015 18:57:47
Problema Arbori indexati binar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.09 kb



#include <fstream>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
#include <string>
#include <cstring>
#include <cstdlib>
#include <bitset>
#include <set>
#include <utility>
#include <vector>
#include <utility>
#include <tr1/unordered_map>

#define mp make_pair
#define pb push_back
#define oo 0x3f3f3f3f
#define f first
#define s second
#define dim 100009

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


int n , arb[dim] , m;

inline int lsb( int x )
{
    return x & -x;

}

void Read();
void update( int poz , int elem );
int sum( int poz );
void cauta( int suma );
void Solve();

int main()
{

    Read();
    Solve();

    return 0;

}

void Read()
{

    in >> n >> m;

    for( int i = 1 ; i<=n ; i++)
    {

        int x;
        in >> x;
        update(i,x);

    }

}

void update(int indice , int elem)
{

    for( int i = indice ; i<=n ; i+=lsb(i))
        arb[i]+=elem;

}

int sum(int indice)
{


    int S = 0;
    for( int i = indice ; i >= 1 ; i-=lsb(i))
        S+=arb[i];

    return S;

}

void cauta(int x)
{

     int i, ok = 1, j = 0;
    for ( i = 1 ; i <= n ; i = i << 1 );
    //g << i;

    while( i > 0 && ok == 1 )
    {
        if ( i + j <= n )
            if ( x >= arb[ i + j ] )
            {
                j = j + i;
                x = x - arb[ j ];

                if ( x == 0 )
                    ok = 0;
            }
            i = i >> 1;
    }

    if ( ok == 0 )
        out << j << "\n";
    else
        out << -1 << "\n";

}

void Solve()
{

    for( int tip , x , y ; m ; --m )
    {

        in >> tip ;

        if( tip == 0 )
        {

            in >> x >> y;
            update(x,y);

        }
        else
            if( tip == 1 )
        {
            in >> x >> y;
            out << sum(y) - sum(x-1) << '\n';
        }
        else
            if ( tip == 2 )
                {
                    in >> x;
                    cauta(x);
                }

    }

}