Cod sursa(job #3198311)

Utilizator Raul_AArdelean Raul Raul_A Data 28 ianuarie 2024 19:41:31
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <bits/stdc++.h>
#define eb emplace_back
#define ll long long
#define pii pair<int, int>
#define pli pair<ll, int>
#define tpl tuple<int, int, int>
#define tli tuple<ll, int, int>
using namespace std;

const string fn("aib");

ifstream in(fn + ".in");
ofstream out(fn + ".out");

#define cin in
#define cout out

const int MAX = 1e5;

int n, m, v[MAX + 5],aibsum[MAX + 5];

void upd(int poz,int val)
{
    for (int i = poz; i <= MAX; i += i & -i)
        aibsum[i] += val;
}

void read()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i];
        upd(i, v[i]);
    }
}

int query(int poz)
{
    int sum=0;
    for(;poz>0;poz-=poz&-poz)
        sum+=aibsum[poz];
    return sum;
}

int binary_lifting(int target)
{
    int pos=0;
    for(int p=1<<19;p;p>>=1)
        if(pos+p<=n and aibsum[pos+p]<=target)
        {
            pos+=p,target-=aibsum[pos];
            if(target==0)
                return pos;
        }
    return -1;
}

void solve()
{
    int t,a,b;
    cin>>t;
    if(t==0)
    {
        cin>>a>>b;
        upd(a,b);
    }
    else if(t==1)
    {
        cin>>a>>b;
        cout<<query(b)-query(a-1)<<'\n';
    }
    else
    {
        ll val;
        cin>>val;
        cout<<binary_lifting(val)<<'\n';
    }
}

int main()
{
    read();
    while(m--) solve();
    return 0;
}