Cod sursa(job #3214870)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 14 martie 2024 15:19:10
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
using pii = pair<int,int>;
const int nmax = 1e5 + 1;
ifstream cin("aib.in");
ofstream cout("aib.out");
int x , bit[nmax] , n , m , op , a , b;
void upd(int x , int &val)
{
    for(; x<=n ; x+=(x&-x)) bit[x] += val;
}
long long qry(int x)
{
    long long sum = 0;
    for(; x>0 ; x-=(x&-x)) sum += bit[x];
    return sum;
}
signed main()
{
    cin >> n >> m;
    for(int i = 1 ; i <= n ; ++i)
    {
        cin >> x;
        upd(i,x);
    }
    for(int i = 1 ; i <= m ; ++i)
    {
        cin >> op >> a;
        if(!op)
        {
            cin >> b;
            upd(a,b);
        }
        if(op&1)
        {
            cin >> b;
            cout << qry(b) - qry(a-1) << '\n';
        }
        if(op==2)
        {
            int st = 1 , dr = n , ans = -1;
            while(st <= dr)
            {
                int mj = st + (dr-st)/2;
                if(qry(mj) >= a)
                {
                    ans = mj;
                    dr = mj - 1;
                }
                else st = mj + 1;
            }
            if(ans == -1)
            {
                cout << -1;
            }
            else if(qry(ans) != a)
            {
                cout << -1;
            }
            else cout << ans;
            cout << '\n';
        }
    }
    return 0;
}