Pagini recente » Cod sursa (job #2549788) | Cod sursa (job #1441420) | Cod sursa (job #1852708) | Cod sursa (job #2766509) | Cod sursa (job #1320256)
#include<stdio.h>
int m,n,aib[100004];
int lsb(int x)
{
return x&(-x);
}
void update(int poz,int val)
{
while(poz<=n)
{
aib[poz]+=val;
poz+=lsb(poz);
}
}
int query(int poz)
{
int ans=0;
while(poz>0)
{
ans+=aib[poz];
poz-=lsb(poz);
}
return ans;
}
int bs(int val)
{
int poz=0;
int vv=val;
for(int b=20;b>=0;b--)
{
if(poz+(1<<b)<=n&&aib[poz+(1<<b)]<val)
{
poz+=(1<<b);
val-=aib[poz];
}
}
if(query(poz+1)==vv)
return poz+1;
else return -1;
}
int main()
{
int a,b,x,i,type;
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%d",&type,&a);
if(type==0)
{
scanf("%d",&b);
update(a,b);
}
if(type==1)
{
scanf("%d",&b);
printf("%d\n",query(b)-query(a-1));
}
if(type==2)
printf("%d\n",bs(a));
}
return 0;
}