Cod sursa(job #1856532)

Utilizator jason2013Andronache Riccardo jason2013 Data 25 ianuarie 2017 00:16:48
Problema Arbori indexati binar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include<bits/stdc++.h>

using namespace std;

ifstream f("aib.in");
ofstream g("aib.out");

const int NMAX = 100001;
int N,M, a[NMAX];

inline void updateIndexedTree(int p, int x)
{
    while(p <= N)
    {
        a[p] += x;
        p = p + (p & (-p));
    }
}

inline int Query(int p)
{
    int s = 0;
    while(p > 0)
    {
        s += a[p];
        p = p - (p & (-p));
    }
    return s;
}

inline int BS(int a)
{
    int st, dr, mid, p, x;
    st = 1;
    dr = N;
    p = -1;
    while(st <= dr)
    {
        mid = (st + dr) / 2;
        x = Query(mid);
        if(x == a)
        {
            p = mid;
            dr = mid - 1;
        }
        else if( x > a)
            dr = mid - 1;
        else st = mid + 1;
    }
    return p;
}

void citire()
{
    f>>N>>M;
    int x, op, p, a, b, k, y;
    for(int i = 1; i <= N; i++)
    {
        f>>x;
        updateIndexedTree(i, x);
    }

    for(int i = 1; i <= M; i ++)
    {
        f >> op;
        if(op == 0)
        {
            f >> p >> x;
            updateIndexedTree(p, x);
        }
        else if(op == 1)
        {
            f >> x >> y;
            g << Query(y) - Query(x - 1) << "\n";
        }
        else
        {
            f >> a;
            g << BS(a) <<"\n";
        }
    }
}

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