Pagini recente » Cod sursa (job #1236622) | Cod sursa (job #1597750)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("cautbin.in");
ofstream cout("cautbin.out");
const int MAX = 100002;
int n, m, v[MAX], rez0, rez1, rez2, t, x;
int caut01(int x)
{
int st = 1;
int dr = n;
while(st<=dr)
{
int mij = (st+dr)/2;
if(v[mij]<=x)
st = mij + 1;
else
dr = mij - 1;
}
return dr;
}
int caut03(int x)
{
int st = 1;
int dr = n;
while(st<=dr)
{
int mij = (st+dr)/2;
if(v[mij]<x)
st = mij + 1;
else
dr = mij - 1;
}
return st;
}
int main()
{
cin >> n;
for(int i=1;i<=n;i++)
cin >> v[i];
cin >> m;
for(int i=1;i<=m;i++){
cin >> t >> x;
if(t==0){
int poz = caut01(x);
if(poz<=n and v[poz]==x)
cout << poz << endl;
else cout << -1 << endl;
}
if(t==1) cout << caut01(x) << endl;
if(t==2) cout << caut03(x) << endl;
}
return 0;
}