Pagini recente » Cod sursa (job #1448237) | Cod sursa (job #1417444) | Cod sursa (job #1379446) | Cod sursa (job #1913539) | Cod sursa (job #1809866)
#include <fstream>
using namespace std;
ifstream in("cautbin.in");
ofstream out("cautbin.out");
const int L=16;
int v[100001];
int caut(int l,int a,int x)
{
int r=0,pas=1<<L;
if(a<=1)
{
while(pas!=0)
{
if(r+pas<=l&&v[r+pas]<=x)
{
r+=pas;
}
pas/=2;
}
}
else
{
while(pas!=0)
{
if(r+pas<=l&&v[r+pas]<x)
{
r+=pas;
}
pas/=2;
}
}
if(a==0)
{
if(v[r]==x){
return r;
}else{
return-1;
}
}
else if(a==1)
{
return r;
}
else if(a==2)
{
return 1+r;
}
}
int main()
{
int n,t,a,b,i;
in>>n;
for(i=1; i<=n; i++)
{
in>>v[i];
}
in>>t;
for(i=1; i<=t; i++)
{
in>>a>>b;
out<<caut(n,a,b)<<'\n';
}
}