Pagini recente » Cod sursa (job #974861) | Cod sursa (job #866660) | Cod sursa (job #2055959) | Cod sursa (job #1880695) | Cod sursa (job #2735462)
#include <fstream>
using namespace std;
ifstream cin ("a.in");
ofstream cout ("a.out");
int n, m, N = 1;
int aib[100005];
int lsb(int x) {
return (x & (-x));
}
void Update(int nod, int val) { //nodul si valoarea pe care o adaug
while (nod <= n) {
aib[nod] += val;
nod += lsb(nod);
}
}
int Query(int nod) {
int ans = 0;
while (nod > 0) {
ans += aib[nod];
nod -= lsb(nod);
}
return ans;
}
int bs(int val) {
int ans = 0, sum = 0;
for (int bit = 16; bit >= 0; --bit) {
if (ans + (1 << bit) <= n && sum + aib[(1 << bit)] <= val) {
ans += (1 << bit);
sum += aib[ans];
}
}
if (sum == val && ans)
return ans;
return -1;
}
int main() {
cin >> n >> m;
int type, x, y;
for (int i = 1; i <= n; ++i) {
cin >> x;
Update(i, x);
}
while (m--) {
cin >> type;
if (type == 0) {
cin >> x >> y;
Update(x, y);
}
if (type == 1) {
cin >> x >> y;
cout << Query(y) - Query(x - 1) << '\n';
}
if (type == 2) {
cin >> x;
cout << bs(x) << '\n';
}
}
return 0;
}