Pagini recente » Istoria paginii runda/concurs_micut | Cod sursa (job #1087130) | Cod sursa (job #2965849) | Cod sursa (job #1609340) | Cod sursa (job #1790742)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int a[100001],n,m;
void Update(int p,int x)
{
while(p<=n)
{
a[p]=a[p]+x;
p=p+(p&(-p));
}
}
int Query(int p)
{
int suma=0;
while(p>=1)
{
suma+=a[p];
p-=(p&(-p));
}
return suma;
}
int Cautare(int suma)
{
int st=1,dr=n,poz,mij,x;
poz=-1;
while(st<=dr)
{
mij=(st+dr)/2;
x=Query(mij);
if(x==suma)
{
poz=mij;
dr=mij-1;
}
else if(x<suma)st=mij+1;
else dr=mij-1;
}
return poz;
}
int main()
{
int i,x,y,op;
fin>>n>>m;
for(i=1;i<=n;i++)
{
fin>>x;
Update(i,x);
}
for(i=1;i<=m;i++)
{
fin>>op;
if(op==0)
{
fin>>x>>y;
Update(x,y);
}
else if(op==1)
{
fin>>x>>y;
fout<<Query(y)-Query(x-1)<<"\n";
}
else
{
fin>>x;
fout<<Cautare(x)<<"\n";
}
}
fin.close();
fout.close();
return 0;
}