Pagini recente » Cod sursa (job #2682513) | Cod sursa (job #1191958) | Cod sursa (job #1099391) | Rating wdwqdwqdwqd (cerct) | Cod sursa (job #1321065)
#include <fstream>
//#include <iostream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int i,val,n,x,stop,a[200000],m,y,j;
int binary_searchh(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_searchhh(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(i=1;i<=n;++i)
fin>>a[i];
fin>>m;
i=1;
while(i<=m)
{
fin>>y>>x;
if(y==0)
{
if(a[binary_searchh(x)]==x)
fout<<binary_searchh(x)<<"\n";
else
fout<<-1<<"\n";
}
if(y==1)
fout<<binary_searchh(x)<<"\n";
if(y==2)
fout<<binary_searchhh(x)<<"\n";
i++;
}
return 0;
}