Pagini recente » Cod sursa (job #802742) | Cod sursa (job #411711) | Cod sursa (job #2359076) | Cod sursa (job #364978) | Cod sursa (job #3309581)
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int bit[N],n,m;
int sum(int pos){
int res=0;
while (pos>0){
res+=bit[pos];
pos=pos&(pos-1);
}
return res;
}
int getsum(int l, int r){
return sum(r)-sum(l);
}
void update(int pos, int b){
while (pos<=n){
bit[pos]+=b;
pos+=pos&(-pos);
}
}
int getk(int a){
int cur=0;
int idx=0, pow2=1;
while ((pow2<<1)<=n)pow2<<=1;
while (pow2){
int i=pow2+idx;
if (i<=n && cur+bit[i]<a){
cur+=bit[i];
idx=i;
}
pow2>>=1;
}
if (idx+1<=n){
idx++;
}
else{
return -1;
}
return(sum(idx)==a?idx:-1);
}
int main()
{
freopen("aib.in", "r", stdin);
freopen("aib.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m;
for (int i=1;i<=n;i++){
int a;cin>>a;
update(i, a);
}
while (m--){
int op;cin>>op;
if (op==0){
int a,b;cin>>a>>b;
update(a,b);
}
else if (op==1){
int a,b;cin>>a>>b;
cout<<getsum(a-1,b)<<'\n';
}
else if (op==2){
int a;cin>>a;
cout<<getk(a)<<'\n';
}
}
return 0;
}