Pagini recente » Cod sursa (job #3287296) | Cod sursa (job #1789203) | Cod sursa (job #305241) | Cod sursa (job #421106) | Cod sursa (job #1789662)
#include <stdio.h>
#define maxdim 100005
int n;
long long aib[maxdim];
inline int lsb(const int &x) {
return x & -x;
}
void update(int poz, int val) {
while (poz <= n) {
aib[poz] += val;
poz += lsb(poz);
}
}
long long query(int poz) {
long long sol = 0;
while (poz > 0) {
sol += aib[poz];
poz -= lsb(poz);
}
return sol;
}
int find_poz_with_sum(int sum) {
int step = 1;
while (step < n) {
step <<= 1;
}
int poz = 0;
for (; step > 0; step >>= 1) {
if (poz + step <= n && aib[poz + step] <= sum) {
poz += step;
sum -= aib[poz];
}
}
if (sum != 0) {
poz = -1;
}
return poz;
}
int main() {
freopen("aib.in", "r", stdin);
freopen("aib.out", "w", stdout);
int ops;
scanf("%d %d", &n, &ops);
for (int i = 1; i <= n; ++i) {
int x; scanf("%d", &x);
update(i, x);
}
while (ops--) {
int type; scanf("%d", &type);
if (type == 0) {
int x, y; scanf("%d %d", &x, &y);
update(x, y);
} else if (type == 1) {
int x, y; scanf("%d %d", &x, &y);
printf("%d\n", (int) (query(y) - query(x - 1)));
} else if (type == 2) {
int sum; scanf("%d", &sum);
printf("%d\n", find_poz_with_sum(sum));
}
}
return 0;
}