Pagini recente » Cod sursa (job #1252173) | Cod sursa (job #2097800) | Cod sursa (job #1200146) | Cod sursa (job #280967) | Cod sursa (job #522292)
Cod sursa(job #522292)
#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],b[N],p;
int x,a;
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;
b[0]=0;
for(i=1;i<=n;i++)
{f1>>a;
b[i]=b[i-1]+a;
j=i;
j-=(j&(-j));
c[i]=b[i]-b[j];}
for(i=1;i<=m;i++)
{f1>>x;
if(x==2)
{f1>>p;
f2<<pozitie(step,p)<<endl;}
else
if(x==0)
{f1>>y>>z;
while(y<=n)
{c[y]+=z;
y+=(y&(-y));}}
else
{f1>>y>>z;
f2<<suma(y,z)<<endl;}}
f1.close();
f2.close();
return 0;}