Pagini recente » Cod sursa (job #386139) | Cod sursa (job #2376186) | Cod sursa (job #456708) | Cod sursa (job #3213486) | Cod sursa (job #3186503)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
const int NMAX = 1e5+5;
int aib[NMAX], n;
void update(int pos, int val) {
for (int i = pos; i <= n; i += i&(-i))
aib[i] += val;
}
int query(int pos) {
int res = 0;
for (int i = pos; i > 0; i -= i&(-i))
res += aib[i];
return res;
}
int query2(int a) {
int pos = 0, s = 0;
for (int b = 20; b >= 0; b--) {
if (pos + (1 << b) > n)
continue;
if (s + aib[pos + (1 << b)] <= a) {
pos += (1 << b);
s += aib[pos + (1 << b)];
}
}
return pos;
}
int main() {
int q;
fin >> n >> q;
for (int i = 1; i <= n; i++) {
int x;
fin >> x;
update(i, x);
}
while (q--) {
int c;
fin >> c;
if (c == 0) {
int a, b;
fin >> a >> b;
update(a, b);
} else if (c == 1) {
int a, b;
fin >> a >> b;
fout << (query(b) - query(a - 1)) << '\n';
} else {
int a;
fin >> a;
fout << query2(a) << '\n';
}
}
return 0;
}