Pagini recente » Cod sursa (job #2524012) | Cod sursa (job #3209308) | Cod sursa (job #1460275) | Cod sursa (job #327479) | Cod sursa (job #3302311)
#include <bits/stdc++.h>
std::ifstream fin("aib.in");
std::ofstream fout("aib.out");
const int MAX_N = 100'000;
const int MAX_LOG = 30;
int aib[MAX_N + 1];
int n, m, op, a, b;
void update(int x, int y) {
for (int i = x; i <= n; i += i & -i)
aib[i] += y;
}
int query(int x) {
int sum = 0;
for (int i = x; i >= 1; i -= i & -i)
sum += aib[i];
return sum;
}
int main() {
fin >> n >> m;
for (int i = 1; i <= n; i++) {
fin >> a;
update(i, a);
}
while (m--) {
fin >> op;
if (op == 0) {
fin >> a >> b;
update(a, b);
} else
if (op == 1) {
fin >> a >> b;
fout << query(b) - query(a - 1) << "\n";
} else
if (op == 2) {
fin >> a;
/// Cautam binar (pe biti) cea mai mica pozitia pos, astfel incat
/// SUM[1..pos] < a. Atunci SUM[1..pos'] >= a, pos' >= pos.
int sum = 0, pos = 0;
for (int b = MAX_LOG; b >= 0; b--)
if ((pos | (1 << b)) <= n && sum + aib[(pos | (1 << b))] < a) {
sum += aib[(pos | (1 << b))];
pos |= (1 << b);
}
if (pos + 1 > n || query(pos + 1) != a) {
fout << "-1\n";
continue;
}
fout << (pos + 1) << "\n";
}
}
fin.close();
fout.close();
return 0;
}