Pagini recente » Cod sursa (job #2670308) | Cod sursa (job #2667902) | Cod sursa (job #469884) | Cod sursa (job #579103) | Cod sursa (job #2379598)
#include <stdio.h>
#include <iostream>
using namespace std;
FILE *in,*out;
int n,m,p;
int arb[100002];
void update(int pos, int val){
int c=0;
while(pos<=n){
arb[pos]+=val;
while(!(pos&(1<<c))) c++;
pos+=1<<c;
c++;
}
}
int query(int pos){
int s=0,c=0;
while(pos){
s+=arb[pos];
while(!(pos&(1<<c))) c++;
pos-=1<<c;
c++;
}
return s;
}
int search(int p)
{
int i,step;
step=1;
while(2*step<=n)
step*=2;
for(i=0;step;step/=2)
{
if(i+step<=n)
{
if(p>=arb[i+step])
{
i+=step;
p-=arb[i];
if(!p)
return i;
}
}
}
return -1;
}
void read(){
int x;
fscanf(in,"%d %d",&n,&m);
for(int i=1;i<=n;i++){
fscanf(in,"%d",&x);
update(i,x);
}
}
void solve(){
int k,a,b;
for(int i=1;i<=m;i++){
fscanf(in,"%d",&k);
if(k<2){
fscanf(in,"%d %d",&a,&b);
if(!k) update(a,b);
else fprintf(out,"%d\n",query(b)-query(a-1));
}
else{
fscanf(in,"%d",&p);
fprintf(out,"%d\n",search(p));
}
}
}
int main(){
in=fopen("aib.in","r");
out=fopen("aib.out","w");
read();
solve();
return 0;
}