Pagini recente » Cod sursa (job #2010833) | Cod sursa (job #1492591) | Cod sursa (job #912466) | Cod sursa (job #2115414) | Cod sursa (job #1402645)
#include <iostream>
#include <cstdio>
#define nmax 100005
#define zeros(x) ( (x ^ (x - 1)) & x )
using namespace std;
int a[nmax],n,m,AIB[nmax];
inline void grow(int x, int q){
for(int i=x;i<=n;i+=zeros(i))
AIB[i]+=q;
}
inline int Compute(int x){
int s=0;
for(int i=x;i>0;i-=zeros(i))
s+=AIB[i];
return s;
}
inline int query1(int x){
int l,r,mij,j=-1,b;
l=1;r=n;
while(l<=r){
mij=(l+r)/2;
b = Compute(mij);
if(b == x){j=mij;r=mij-1;}
else if(b < x)l=mij+1;
else r=mij-1;
}
return j;
}
int main(){
int i,key,x,y;
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%d%d\n",&n,&m);
for(i=1;i<=n;++i)
{scanf("%d ",&a[i]);grow(i,a[i]);}
while(m--){
scanf("%d",&key);
if(key == 2){
scanf("%d\n",&x);
printf("%d\n",query1(x));
continue;
}
scanf("%d%d\n",&x,&y);
if(!key) {grow(x,y);continue;}
printf("%d\n",(Compute(y)-Compute(x-1)));
}
return 0;
}