Pagini recente » Cod sursa (job #2657043) | Cod sursa (job #1067886) | Cod sursa (job #2625078) | Cod sursa (job #2706807) | Cod sursa (job #2379590)
#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 pos=n+1;
if(query(n)==p)
return n;
int st=0,dr=n+1,mij=n;
while(mij){
mij=(st+dr)>>1;
int aux=query(mij);
if(aux==p)
pos=min(pos,mij),dr=mij,mij--;
else if(aux<p){
if(st<mij)
st=mij;
mij++;
}
else{
if(dr>mij)
dr=mij;
mij--;
}
if(mij<=st) break;
if(mij>=dr) break;
}
if(pos==n+1)
pos=-1;
return pos;
}
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;
}