Pagini recente » Cod sursa (job #1848471) | Cod sursa (job #2363171) | Cod sursa (job #779798) | Cod sursa (job #1985962) | Cod sursa (job #469557)
Cod sursa(job #469557)
#include<cstdio>
#define N 1<<17
int n,m,v[N];
int bit(int x)
{
return ((x&(x-1))^x);
}
void update(int poz,int x)
{
while(poz<=n)
{
v[poz]+=x;
poz+=bit(poz);
}
}
unsigned int suma(int poz)
{
unsigned int s=0;
while(poz)
{
s+=v[poz];
poz-=bit(poz);
}
return s;
}
void cautb(unsigned int x)
{
int i,poz=0;
for(i=1<<20;i;i>>=1)
{
if(poz+i>n)
continue;
poz+=i;
unsigned int s=suma(poz);
if(s==x)
{
printf("%d\n",poz);
return;
}
if(s<x)
continue;
poz-=i;
}
}
int main()
{
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%d%d",&n,&m);
int x,y,t;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
update(i,x);
}
for(int i=1;i<=m;i++)
{
scanf("%d",&t);
if(t==0)
{
scanf("%d%d",&x,&y);
update(x,y);
}
else if(t==1)
{
scanf("%d%d",&x,&y);
printf("%d\n",(unsigned int)suma(y)-suma(x-1));
}
else if(t==2)
{
scanf("%d",&x);
cautb(x);
}
}
return 0;
}