Pagini recente » Cod sursa (job #2708817) | Cod sursa (job #2975670) | Cod sursa (job #923733) | Cod sursa (job #2534646) | Cod sursa (job #1459866)
#include <iostream>
#include <fstream>
#define MAX 100005
using namespace std;
long tree[MAX];
int n, B;
void update (int pos, long val) {
while (pos <= n) {
tree[pos] += val;
pos += pos & -pos;
}
}
long query (int pos) {
long s = 0;
while (pos) {
s += tree[pos];
pos -= pos & -pos;
}
return s;
}
long find (long val) {
int bitmask = B, i = 0;
while (bitmask && (i < n)) {
int pos = bitmask + i;
if (pos <= n) {
if (val >= tree[pos]) {
val -= tree[pos];
i += bitmask;
if (!val) return pos;
}
}
bitmask >>= 1;
}
return -1;
}
int main (void) {
ios::sync_with_stdio(false);
freopen("aib.in", "r", stdin);
freopen("aib.out", "w", stdout);
int m;
cin >> n >> m;
for (B = 1; B < n; B <<= 1);
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
update(i, x);
}
for (int i = 0; i < m; ++i) {
int x, a, b;
cin >> x;
if (x < 2) {
cin >> a >> b;
if (!x) {
update(a, b);
} else {
cout << query(b) - query(a-1) << "\n";
}
} else {
cin >> a;
cout << find(a) << "\n";
}
}
return 0;
}