Mai intai trebuie sa te autentifici.

Cod sursa(job #3367988)

Utilizator RuxandraPro12_Metehau Ruxandra Maria RuxandraPro12_ Data 12 octombrie 2026 16:34:56
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("aib.in");
ofstream fout ("aib.out");

const int NMAX = 1e5;

int n, m, aib[NMAX + 5];

void update (int poz, int val) {
    while (poz <= n) {
        aib[poz] += val;
        int p2 = (poz & (-poz));
        poz += p2;
    }
}

int query (int poz) {
    int cnt = 0, s = 0;
    while (poz > 0) {
        s += aib[poz];
        int p2 = (poz & (-poz));
        poz -= p2;
    }
    return s;
}

int cautare_poz (int k) {
    int i, step;
    for (step = 1; step < n; step <<= 1);
    for (i = 0; step; step >>= 1) {
        if (i + step <= n && k >= aib[i + step]) {
            i += step;
            k -= aib[i];
            if (!k)
                return i;
        }
    }
    return -1;
}

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