Cod sursa(job #3197736)

Utilizator BurloiEmilAndreiBurloi Emil Andrei BurloiEmilAndrei Data 27 ianuarie 2024 13:09:42
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define pb push_back

const string FILE_NAME = "aib";
const int MAX_N = 1e5;

int aib[MAX_N + 5], n, v[MAX_N + 5];

int query(int x){
    int rez;
    rez=0;
    for(; x>=1; x&=(x-1))
        rez+=aib[x];
    return rez;
}

void update(int x, int y){
    for(; x<=n; x+=x&(-x))
        aib[x]+=y;
}

int main() {
#ifndef LOCAL
    ifstream cin(FILE_NAME + ".in");
    ofstream cout(FILE_NAME + ".out");
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int m, a, b, st, dr, mij;

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

    for (int i = 0; i < m; i++) {
        int cer;

        cin >> cer;
        if (cer == 0) {
            cin >> a >> b;
            update(a, b);
        } else if (cer == 1) {
            cin >> a >> b;
            cout << query(b) - query(a - 1) << '\n';
        } else {
            cin >> a;

            st = 1, dr = n;
            while (st <= dr) {
                mij = (st + dr) / 2;

                if (query(mij) < a) {
                    st = mij + 1;
                } else {
                    dr = mij - 1;
                }
            }
            if (query(dr + 1) == a) {
                cout << dr + 1;
            } else {
                cout << "-1";
            }
            cout << '\n';
        }
    }
    return 0;
}