Pagini recente » Cod sursa (job #3290066) | Viata de dupa olimpiade? (partea I) | Cod sursa (job #1655958) | Cod sursa (job #3261176) | Cod sursa (job #3284946)
#include <fstream>
#define nmax 100001
using namespace std;
ifstream cin("aib.in");
ofstream cout("aib.out");
int n,q,aib[nmax],a,b,task;
void update(int p,int x){
for(;p<=n;p+=(-p)&p)
aib[p]+=x;
}
int query(int p){
int r=0;
for(;p>0;p-=(-p)&p)
r+=aib[p];
return r;
}
int cb(int x){
int st=1,dr=n,p=-1;
while(st<=dr){
int mid=(st+dr)/2,val=query(mid);
if(val==x)
p=mid,dr=mid-1;
else if(val<x)
st=mid+1;
else
dr=mid-1;
}
return p;
}
int main()
{
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a;
update(i,a);
}
while(q--){
cin>>task>>a;
if(task==2){
cout<<cb(a)<<'\n';
continue;
}
cin>>b;
if(task==0)
update(a,b);
else
cout<<query(b)-query(a-1)<<'\n';
}
return 0;
}