Pagini recente » Cod sursa (job #2160) | Cod sursa (job #144504) | Cod sursa (job #4169) | Cod sursa (job #11356) | Cod sursa (job #2952483)
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 100005;
int aib[Nmax];
int n, m;
void update(int pos, int val) {
while (pos <= n) {
aib[pos] += val;
pos += (pos & (-pos));
}
}
int query(int pos) {
int sum = 0;
while (pos > 0) {
sum += aib[pos];
pos -= (pos & (-pos));
}
return sum;
}
int bin_search(int a) {
int lo = 1, hi = n;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
int sum = query(mid);
if (sum == a)
return mid;
if (sum < a)
lo = mid + 1;
else
hi = mid - 1;
}
return -1;
}
int main() {
string filename = "aib";
ifstream cin(filename + ".in");
ofstream cout(filename + ".out");
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
update(i, x);
}
while(m--) {
int op, x, y;
cin >> op;
if (op == 0) {
cin >> x >> y;
update(x, y);
}
else if (op == 1) {
cin >> x >> y;
cout << query(y) - query(x - 1) << '\n';
}
else {
cin >> x;
cout << bin_search(x) << '\n';
}
}
return 0;
}