Cod sursa(job #3188118)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 1 ianuarie 2024 19:07:05
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("aib.in");
ofstream fout("aib.out");
int n, t, i, a[100002], x, y;

static inline int ub(int nr) {
    return (nr & -nr);
}

static inline void update(int poz, int val) {
    for(int i = poz; i <= n; i += ub(i)) a[i] += val;
}

static inline int query(int poz) {
    int sum = 0;
    for(int i = poz; i >= 1; i -= ub(i)) sum += a[i];
    return sum;
}

static inline int cb(int val) {
    int st = 1;
    int dr = n;
    int poz = 0;
    while(st <= dr) {
        int mij = (st + dr) / 2;
        if(val <= query(mij)) {
            poz = mij;
            dr = mij - 1;
        }
        else st = mij + 1;
    }
    return poz;
}

int main() {
    fin >> n >> t;
    for(i = 1; i <= n; i++) {
        fin >> x;
        update(i, x);
    }
    while(t--) {
        fin >> x;
        if(x == 0) {
            fin >> x >> y;
            update(x, y);
        }
        else if(x == 1) {
            fin >> x >> y;
            fout << query(y) - query(x - 1) << "\n";
        }
        else {
            fin >> x;

            int poz = cb(x);

            if(a[poz] == x) fout << poz << "\n";
            else fout << "-1\n";
        }
    }

    return 0;
}