Pagini recente » Cod sursa (job #2622180) | Cod sursa (job #975234) | Cod sursa (job #1955325) | Borderou de evaluare (job #2010930) | Cod sursa (job #3232150)
#include <bits/stdc++.h>
using namespace std;
const int N = (int) 1e5 + 7;
int n, m, aib[N];
void add(int i, int x) {
while (i <= n) {
aib[i] += x;
i += i & (-i);
}
}
int get(int i) {
int sol = 0;
while (i) {
sol += aib[i];
i -= i & (-i);
}
return sol;
}
int main() {
#ifdef INFOARENA
freopen ("aib.in", "r", stdin);
freopen ("aib.out", "w", stdout);
#endif
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
add(i, x);
}
while (m--) {
int tp;
cin >> tp;
if (tp == 0) {
int i, x;
cin >> i >> x;
add(i, x);
continue;
}
if (tp == 1) {
int l, r;
cin >> l >> r;
cout << get(r) - get(l - 1) << "\n";
continue;
}
assert(tp == 2);
int wn, pos = 0, ori;
cin >> wn;
ori = wn;
for (int s = (1 << 20); s; s /= 2) {
if (pos + s <= n && aib[pos + s] < wn) {
pos += s;
wn -= aib[pos];
}
}
pos++;
if (get(pos) == ori) {
cout << pos << "\n";
} else {
cout << "-1\n";
}
}
return 0;
}