Pagini recente » Cod sursa (job #402205) | Cod sursa (job #2914348) | Cod sursa (job #1352830) | Cod sursa (job #320484) | Cod sursa (job #1166211)
#include <cstdio>
using namespace std;
int N,M,AIB[100005],tipOp,a,b,i;
long long x;
int cb(int x,int L,int R)
{
if (L>R) return -1;
int M=(L+R)>>1;
if (AIB[M]==x)
{
register int j=M;
while (AIB[--j]==M);
return j+1;
}
else if (AIB[M]>x) cb(x,L,M-1);
else if (AIB[M]<x) cb(x,M+1,R);
}
void update(int i,int x)
{
for (;i<=N;i+=i&-i) AIB[i]+=x;
}
int query(int x)
{
int S=0;
for (;x;x-=x&-x) S+=AIB[x];
return S;
}
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%d %d",&N,&M);
for (i=1;i<=N;i++)
{
scanf("%d",&x);
update(i,x);
}
for (i=1;i<=M;i++)
{
scanf("%d",&tipOp);
if (tipOp==0)
{
scanf("%d %d",&a,&b);
update(a,b);
}
else if (tipOp==1)
{
scanf("%d %d",&a,&b);
printf("%d\n",query(b)-query(a-1));
}
else
{
scanf("%d",&x);
printf("%d\n",cb(x,1,N));
}
}
return 0;
}