Pagini recente » Cod sursa (job #3318526) | Cod sursa (job #3338159) | Cod sursa (job #570412) | Cod sursa (job #1940336) | Cod sursa (job #3342073)
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
#define cin fin
#define cout fout
const int NMAX=100000;
int N, v[NMAX+1];
int caut0(int x)
{
int p=1, u=N, poz=-1;
while (p<=u)
{
int m=(p+u)/2;
if (x==v[m])
{
poz=m;
p=m+1;
}
else
if (x>v[m])
p=m+1;
else u=m-1;
}
return poz;
}
int caut1(int x)
{
int p=1, u=N, poz=-1;
while (p<=u)
{
int m=(p+u)/2;
if (x>=v[m])
{
poz=m;
p=m+1;
}
else u=m-1;
}
return poz;
}
int caut2(int x)
{
int p=1, u=N, poz=-1;
while (p<=u)
{
int m=(p+u)/2;
if (x<=v[m])
{
poz=m;
u=m-1;
}
else p=m+1;
}
return poz;
}
int main()
{
int M, x, t;
cin>>N;
for (int i=1; i<=N; ++i)
cin>>v[i];
cin>>M;
while (M--)
{
cin>>t>>x;
switch(t)
{
case 0:
cout<<caut0(x)<<'\n';
break;
case 1:
cout<<caut1(x)<<'\n';
break;
case 2:
cout<<caut2(x)<<'\n';
break;
}
}
return 0;
}