Pagini recente » Cod sursa (job #1312291) | Monitorul de evaluare | Cod sursa (job #2602269) | Cod sursa (job #204332) | Cod sursa (job #522432)
Cod sursa(job #522432)
#include<stdio.h>
#define N 100001
long n,m,y,z,i,j,step,c[N],a[N];
int x;
long suma(long y,long z)
{long s1=0,s2=0;
while(z>0)
{s1+=c[z];
z-=(z&(-z));}
while(y>0)
{s2+=c[y];
y-=(y&(-y));}
return s1-s2;}
long pozitie(long step,long p)
{for(i=0;step!=0;step>>=1)
if(i+step<=n)
if(c[i+step]<p)
{i+=step;
p-=c[i];}
else
if(c[i+step]==p)
return i+step;
return -1;}
int main()
{freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
scanf("%ld%ld",&n,&m);
for(step=1;step<=n;step<<=1);
step>>=1;
a[0]=0;
for(i=1;i<=n;i++)
{scanf("%ld",&a[i]);
a[i]+=a[i-1];
j=i-(i&(-i));
c[i]=a[i]-a[j];}
for(i=1;i<=m;i++)
{scanf("%d%ld",&x,&y);
if(x==2)
printf("%ld\n",pozitie(step,y));
else
{scanf("%ld",&z);
if(x==0)
{while(y<=n)
{c[y]+=z;
y+=(y&(-y));}}
else
printf("%ld\n",suma(y-1,z));}}
fclose(stdin);
fclose(stdout);
return 0;}