Cod sursa(job #2618370)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 24 mai 2020 18:58:01
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <bits/stdc++.h>
#define nmax 100005
#define zeros(x) x & (-x)

using namespace std;

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

int n, m, x, put, tip, a, b, aib[nmax];

void Update (int x, int val) {
    for (int i = x; i <= n; i += zeros(i)) aib[i] += val;
}

int Compute (int x) {
    int suma = 0;
    for (int i = x; i > 0; i -= zeros(i)) suma += aib[i];
    return suma;
}

int Cb (int x) {
    int st = 0;
    if (x == 0) return -1;
    for (int dr = put; dr > 0; dr >>= 1)
    if (st + dr <= n) {
        if (aib[st + dr] <= x) {
            x -= aib[st + dr];
            st += dr;
        }
        if (x == 0) return st;
    }
    return -1;
}


int main() {
    ios::sync_with_stdio (false);
    fin.tie (0);
    fout.tie (0);
    fin >> n >> m;
    put = 1;
    while (put <= n) put <<= 1;
    put >>= 1;
    for (int i = 1; i <= n; i ++) {
        fin >> x;
        Update (i, x);
    }
    while (m --) {
        fin >> tip;
        if (tip != 2) {
            fin >> a >> b;
            if (tip == 0) Update (a, b);
            else fout << Compute (b) - Compute (a - 1) << '\n';
        } else {
            fin >> a;
            fout << Cb (a) << '\n';
        }
    }
    fin.close ();
    fout.close ();
    return 0;
}