Cod sursa(job #2969866)

Utilizator sandry24Grosu Alexandru sandry24 Data 23 ianuarie 2023 20:26:52
Problema Arbori indexati binar Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second

ll n, m;
ll BIT[100001];

void add(ll x, ll delta){
    for(; x <= n; x += x & -x)
        BIT[x] += delta;
}

ll sum(ll x){
    ll sum = 0;
    for(; x > 0; x -= x & -x)
        sum += BIT[x];
    return sum;
}

void solve(){
    cin >> n >> m;
    int a[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        add(i+1, a[i]);
    }
    for(int i = 0; i < m; i++){
        ll c, a, b;
        cin >> c;
        if(c == 0){
            cin >> a >> b;
            add(a, b);
        } else if(c == 1){
            cin >> a >> b;
            cout << sum(b) - sum(a-1) << '\n';
        } else {
            cin >> a;
            int l = 1, r = n;
            while(l != r){
                int mid = (l+r)/2;
                ll suma = sum(mid);
                if(suma == a){
                    l = mid;
                    break;
                } else if(suma < a)
                    l = mid+1;
                else
                    r = mid-1;
            }
            if(sum(l) == a)
                cout << l << '\n';
            else
                cout << -1 << '\n';
        }
    }
}  
 
int main(){
    freopen("aib.in", "r", stdin);
    freopen("aib.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}