Pagini recente » Cod sursa (job #2885035) | Cod sursa (job #427677) | Cod sursa (job #2208686) | Cod sursa (job #107672) | Cod sursa (job #2877823)
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5;
const int logn = 18;
inline int lsb(int x) {
return x & (-x);
}
int n, m, v[nmax+5], aib[nmax+5];
void update(int pos, int val) {
v[pos] += val;
for(int i=pos; i<=n; i+=lsb(i))
aib[i] += val;
}
int prefixSum(int pos) {
int temp = 0;
for(int i=pos; i>=1; i-=lsb(i))
temp += aib[i];
return temp;
}
int query(int sum) {
int pos = 0;
int now = 0;
for(int i=logn; i>=0; i--)
if(pos + (1<<i) <= n and now + aib[pos+(1<<i)]<=sum) {
pos += (1<<i);
now += aib[pos];
}
if(now==sum and pos) return pos;
else return -1;
}
int main() {
ifstream f("aib.in");
ofstream g("aib.out");
f >> n >> m;
for(int i=1; i<=n; i++) {
f >> v[i];
update(i, v[i]);
}
for(int i=1; i<=m; i++) {
int op, a, b; f >> op >> a;
if(op==0) f >> b, update(a, b);
else if(op==1) f >> b, g << prefixSum(b) - prefixSum(a-1) << "\n";
else g << query(a) << "\n";
}
return 0;
}