Pagini recente » Cod sursa (job #1843407) | Cod sursa (job #134581) | Cod sursa (job #3262966) | Cod sursa (job #234255) | Cod sursa (job #522298)
Cod sursa(job #522298)
#include<stdio.h>
#include<math.h>
#include<iostream.h>
#include<fstream.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));}
y--;
while(y>0)
{s2+=c[y];
y-=(y&(-y));}
return s1-s2;}
long pozitie(long step,long p)
{long i;
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()
{fstream f1("aib.in",ios::in);
fstream f2("aib.out",ios::out);
f1>>n>>m;
for(step=1;step<=n;step<<=1);
step>>=1;
a[0]=0;
for(i=1;i<=n;i++)
{f1>>a[i];
a[i]+=a[i-1];
j=i-(i&(-i));
c[i]=a[i]-a[j];}
for(i=1;i<=m;i++)
{f1>>x>>y;
if(x==2)
f2<<pozitie(step,y)<<endl;
else
{f1>>z;
if(x==0)
{while(y<=n)
{c[y]+=z;
y+=(y&(-y));}}
else
f2<<suma(y,z)<<endl;}}
f1.close();
f2.close();
return 0;}