Pagini recente » Cod sursa (job #2821686) | Cod sursa (job #523779) | Cod sursa (job #2895304) | Cod sursa (job #311300) | Cod sursa (job #431015)
Cod sursa(job #431015)
#include <fstream>
using namespace std;
#define MAXN 100005
fstream fin("aib.in",ios::in);
fstream fout("aib.out",ios::out);
int c[MAXN],n,m;
void update(int ind,int val){
int poz=0;
while(ind<=n){
c[ind]+=val;
while(!(ind&(1<<poz))){
poz++;
}
ind+=(1<<poz);
poz++;
}
}
int query(int ind){
int s=0,poz=0;
while(ind>0){
s+=c[ind];
while(!(ind&(1<<poz))){
poz++;
}
ind-=(1<<poz);
poz++;
}
return s;
}
int search(int sum){
int step=1;
while(step<n){
step<<=1;
}
for(int i=0;step;step>>=1){
if(i+step<=n){
if(sum>=c[i+step]){
i+=step,sum-=c[i];
if(!sum){
return i;
}
}
}
}
return -1;
}
int main(){
int op,a,b,d;
fin>>n>>m;
for(int i=1;i<=n;i++){
fin>>d;
update(i,d);
}
for(int i=0;i<m;i++){
fin>>op>>a;
switch(op){
case 0:
fin>>b;
update(a,b);
break;
case 1:
fin>>b;
fout<<query(b)-query(a-1)<<'\n';
break;
case 2:
fout<<search(a)<<'\n';
break;
}
}
return 0;
}