Pagini recente » Cod sursa (job #3169281) | Cod sursa (job #1658783) | Cod sursa (job #3180375) | Cod sursa (job #2820388) | Cod sursa (job #1321096)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int N,A[100010],i,j,x,y,l,u,M;
int binary_search(int val)
{
int i,step;
for(step=1;step<N;step<<=1);
for(i=0;step;step>>=1)
if(i+step<=N && A[i+step]<=val)
i+=step;
return i;
}
int binary_search1(int val)
{
int i,step;
for(step=1;step<N;step<<=1);
for(i=0;step;step>>=1)
if(i+step<=N && A[i+step]<val)
i+=step;
return i+1;
}
int main()
{
fin>>N;
for(j=1;j<=N;j++)
fin>>A[j];
fin>>M;
for(j=0;j<M;j++)
{
fin>>x>>y;
if(x==0)
{
l=binary_search(y);
if(A[l]==y)
fout<<l<<'\n';
else
fout<<"-1"<<'\n';
}
if(x==1)
fout<<binary_search1(y+1)-1<<'\n';
if(x==2)
fout<<binary_search(y-1)+1<<'\n';
}
return 0;
}