Pagini recente » Cod sursa (job #2632245) | Cod sursa (job #737660) | Cod sursa (job #2330589) | Cod sursa (job #2152960) | Cod sursa (job #2413649)
#include <iostream>
#include <fstream>
#define NMAX 100000
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
const int nmax=1e5;
int n,m,x;
int v[nmax+5];
int BS0(int x,int st,int dr){
int rez=-1;
while(st<=dr)
{
int mij=st+(dr-st)/2;
if(v[mij]==x)
{
rez=mij;
st=mij+1;
}
else if(v[mij]>x)
dr=mij-1;
else
st=mij+1;
}
return rez;
}
int BS1(int x,int st,int dr){
int rez=-1;
while(st<=dr)
{
int mij=st+(dr-st)/2;
if(v[mij]<=x)
{
rez=mij;
st=mij+1;
}
else
dr=mij-1;
}
return rez;
}
int BS2(int x,int st,int dr){
int rez=-1;
while(st<=dr)
{
int mij=st+(dr-st)/2;
if(v[mij]>=x)
{
rez=mij;
dr=mij-1;
}
else
st=mij+1;
}
return rez;
}
int main()
{
fin>>n;
for(int i=1;i<=n;++i)
fin>>v[i];
fin>>m;
for(int i=1;i<=m;++i){
int q;
fin>>q>>x;
if(q==0)
fout << BS0(x,1,n)<<"\n";
if(q==1)
fout << BS1(x,1,n)<<"\n";
if(q==2)
fout << BS2(x,1,n)<<"\n";
}
return 0;
}