Pagini recente » Cod sursa (job #1971047) | Cod sursa (job #2784379) | Cod sursa (job #2087539) | Cod sursa (job #1289179) | Cod sursa (job #1076036)
#include<stdio.h>
#include <fstream>
#include <algorithm>
int n,aib[101],pas=1<<16;
int query(int p){
int s=0;
while(p!=0){
s+=aib[p];
p-=p&(-p);
}
return s;
}
void update(int p,int val){
while(p<=n){
aib[p]+=val;
p+=p&(-p);
}
}
int cautbin(int x,int val){
int pas = 1<<16;
while(pas!=0){
if(x+pas<=n&&aib[x+pas]<=val){
val-= aib[x+pas];
x+=pas;
}
pas/=2;
}
if(x==0){
return -1;
}
if(val==0){
return x;
}
return -1;
}
int main(){
int tip,x,m,a,b;
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&x);
update(i,x);
}
for(int i=1;i<=m;i++){
scanf("%d",&tip);
if(tip<=1){
scanf("%d%d",&a,&b);
if(tip==0) update(a,b);
else if(tip==1){
printf("%d\n",query(b)-query(a-1));
}
}
else{
scanf("%d",&a);
printf("%d\n",cautbin(0,a));
}
}
return 0;
}