Pagini recente » Cod sursa (job #2718225) | Cod sursa (job #396209) | Cod sursa (job #1580875) | Cod sursa (job #1560162) | Cod sursa (job #2379357)
#include <stdio.h>
#include <iostream>
#define mij (st+dr)/2
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=1,dr=n;
while(st<=dr){
if(query(mij)==p)
pos=min(pos,mij);
else if(query(mij)<p)
st=mij+1;
else
dr=mij-1;
}
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(a)-query(b-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;
}