Pagini recente » Cod sursa (job #931063) | Cod sursa (job #2466528) | Cod sursa (job #2392991) | Cod sursa (job #1883281) | Cod sursa (job #2396769)
#include <fstream>
using namespace std;
#define maxn 100001
ifstream cin("aib.in");
ofstream cout("aib.out");
int N,M,aib[maxn],op;
void update(int poz, int val){
while(poz<=N){
aib[poz]+=val;
poz+=(poz&-poz);
}
}
int query(int poz){
int suma=0;
while(poz>0){
suma+=aib[poz];
poz-=(poz&-poz); /// exemplu 7 in binar 111 negatul lui e 001 si rezultatul (7&-7)=1
/// 111
/// 001 &
/// 001
/// It's the same with:
/// int c=0;
/// while(!(poz&(1<<c))) c++;
/// poz+=(1<<c);
/// c++;
}
return suma;
}
int bs(int val){
int i,step;
for(step=1; step<N; step<<=1);
for(i=0; step; step >>=1){
if(i+step<=N){
if(val>=aib[i+step]){
i+=step, val-= aib[i];
if(!val) return i;
}
}
}
return -1;
}
void solve(int op){
int x,y;
if(op==0){
cin>>x>>y;
update(x,y);
}else if(op==1){
cin>>x>>y;
cout<<query(y)-query(x-1)<<'\n';
}else if(op==2){
cin>>x;
cout<<bs(x)<<'\n';
}
}
int main(){
int x;
cin>>N>>M;
for(int i=1; i<=N; i++){
cin>>x;
update(i,x);
}
for(int i=1; i<=M; i++){
cin>>op;
solve(op);
}
return 0;
}