Cod sursa(job #2350448)

Utilizator tiberiu392Tiberiu Ungurianu tiberiu392 Data 21 februarie 2019 12:53:56
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.47 kb
#include <fstream>
#define Nmax 100005
#define lsd(x) ((x^(x-1))&x)
using namespace std;
ifstream f("datorii.in");
ofstream g("datorii.out");
int n, m, x, t, y, pwr=1, i, j, caz, ok;
int arb[Nmax];

void update( int i, int x)
{
    for( ; i <= n ; i += lsd(i) )
    {
        arb[i] += x;
    }
}

int query( int i)
{
    int suma = 0;
    while ( i )
    {
        suma += arb[i];
        i -= (i&-i);
    }
    return suma;
}


int main()
{
    f >> n >> m;
    while ( pwr < n )
        pwr *= 2;
        for( i = 1 ; i <= n ; i++ )
        {
             f >> x ;
             update( i, x );
        }
    while ( m-- )
    {
        f >> caz;
        if ( caz == 0 )
        {
            f >> x >> y ;
            update( x , y);
        }
        else
            if( caz == 1 )
        {
            f >> x >> y;
            g << query(y)-query(x-1) << "\n";
        }
        else
        if( caz == 2 )
        {
            f >> x;
            ok = false;
            int p = pwr, j = 0 ;

        for ( ; p ; p /= 2)
        {
            if( j+p > n )
                continue;
            y = query(j+p);
            if ( x == y )
            {
                ok = true;
                g << j+p << "\n";
                break;
            }
            else
                if( y < x )
                j += p;
        }
        if( ok == false )
            g << "-1\n";
        }
    }
    return 0;
}