Pagini recente » Cod sursa (job #2834268) | Cod sursa (job #974123) | Cod sursa (job #1719101) | Cod sursa (job #859552) | Cod sursa (job #1145627)
#include <cstdio>
using namespace std;
int n,m,i,cod,x,y,step,pos,Step,bit,query(int),AIB[100010];
void upd(int,int);
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%d%d",&n,&m);
for(step=1;;step<<=1)if(step>n)break;step>>=1;
for(i=1;i<=n;i++)
{
scanf("%d",&x);
upd(i,x);
}
for(;m;m--)
{
scanf("%d%d",&cod,&x);
if(cod==0)
{
scanf("%d",&y);
upd(x,y);
continue;
}
if(cod==1)
{
scanf("%d",&y);
printf("%d\n",query(y)-query(x-1));
continue;
}
for(pos=0,Step=step;Step;Step>>=1)
if(AIB[pos+Step]&&AIB[pos+Step]<=x)
{
pos+=Step;
x-=AIB[pos];
}
x?printf("-1\n"):pos?printf("%d\n",pos):printf("-1\n");
}
return 0;
}
void upd(int poz,int val)
{
for(;poz<=n;)
{
bit=poz&(-poz);
AIB[poz]+=val;
poz+=bit;
}
}
int query(int poz)
{
int ret=0;
for(;poz;)
{
bit=poz&(-poz);
ret+=AIB[poz];
poz-=bit;
}
return ret;
}