Pagini recente » Cod sursa (job #1239742) | Cod sursa (job #1486814) | Cod sursa (job #1049429) | Cod sursa (job #1192597) | Cod sursa (job #1049667)
#include <stdio.h>
#include <iostream>
using namespace std;
#define LSB(x) ((-x)&x)
int bit[100001];
void BIT_update(int pos,int val,int limit)
{
while(pos<=limit)
{
bit[pos]+=val;
pos+=LSB(pos);
}
}
int BIT_query(int pos)
{
int sum=0;
while(pos>0)
{
sum+=bit[pos];
pos-=LSB(pos);
}
return sum;
}
int BIT_search(int sum,int limit)
{
int pow=0,pos=0;
while((1LL<<pow)<=limit) ++pow;
--pow;
for(;pow>=0;--pow)
if(pos+(1<<pow)<=limit)
if(bit[pos+(1<<pow)]<=sum)
{
sum-=bit[pos+(1<<pow)];
pos+=(1<<pow);
if(sum==0) return pos;
}
return -1;
}
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
int N,M;
scanf("%d%d",&N,&M);
for(int i=1;i<=N;++i)
{
int val;
scanf("%d",&val);
BIT_update(i,val,N);
}
while(M--)
{
int op;
scanf("%d",&op);
switch(op)
{
case 0:
int pos,val;
scanf("%d%d",&pos,&val);
BIT_update(pos,val,N);
break;
case 1:
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",BIT_query(b)-BIT_query(a-1));
break;
case 2:
int k;
scanf("%d",&k);
printf("%d\n",BIT_search(k,N));
break;
}
}
return 0;
}