Cod sursa(job #2890398)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 15 aprilie 2022 14:42:16
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <fstream>
#define lsb(x) x & (-x)
#define int long long

using namespace std;

ifstream cin ("aib.in");
ofstream cout ("aib.out");

const int N = 1e5 + 5;
int aib[N];

int n, q, x;

void update (int pos, int val)
{
    for (int i = pos; i <= n; i += lsb(i))
        aib[i] += val;
}

int query (int pos)
{
    int s = 0;
    for (int i = pos; i >= 1; i -= lsb(i))
        s += aib[i];
    return s;
}
struct cerin
{
    int c;
    int st, dr;
} o;

int binarylifting (int val)
{
    int pas = 1;
    while (pas <= n)
        pas <<= 1;
    for (int i = 0; pas; pas >>= 1)
        if (i + pas <= n)
            if (val >= aib[i + pas])
            {
                i += pas;
                val -= aib[i];
                if (!val)
                    return i;
            }
    return -1;
}
signed main()
{
    cin >> n >> q;
    for (int i = 1; i <= n; ++i)
        cin >> x, update (i, x);
    for (int i = 1; i <= q; ++i)
    {
        cin >> o.c >> o.st;
        if (!o.c)
        {
            cin >> o.dr;
            update (o.st, o.dr);
        }
        else if (o.c == 1)
        {
            cin >> o.dr;
            cout << query (o.dr) - query (o.st - 1) << '\n';
        }
        else
        {
            cout << binarylifting(o.st) << '\n';
        }

    }

    return 0;
}