Pagini recente » Cod sursa (job #2317422) | Cod sursa (job #3178867) | Cod sursa (job #311638) | Cod sursa (job #2927532) | Cod sursa (job #1321074)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int N,A[100],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=0;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+1<<'\n';
else
fout<<"-1"<<'\n';
}
if(x==1)
if(binary_search(y)==binary_search1(y))
{
l=binary_search(y);
fout<<l+2<<'\n';
}
else
{
l=binary_search(y);
fout<<l+1<<'\n';
}
if(x==2)
{
l=binary_search1(y);
if(A[l+1]==y)
fout<<l+1<<'\n';
}
}
return 0;
}