Pagini recente » Cod sursa (job #2539761) | Cod sursa (job #725312) | Cod sursa (job #3138550)
#include <fstream>
#include <vector>
using namespace std;
ifstream cin ("cautbin.in");
ofstream cout ("cautbin.out");
vector <int> v;
bool found;
int cerinta;
int x;
int binSrch01 (int st, int dr, int val)
{
int rez = -1;
while (st <= dr)
{
int m = (st + dr) / 2;
if (v[m] <= val)
{
st = m + 1;
rez = m;
found = true;
}
else
dr = m - 1;
}
return rez;
}
int binSrch2 (int st, int dr, int val)
{
int rez = -1;
while (st <= dr)
{
int m = (st + dr) / 2;
if (v[m] >= val)
{
dr = m - 1;
rez = m;
found = true;
}
else
st = m + 1;
}
return rez;
}
struct tests
{
int type;
int val;
};
vector <tests> teste;
int main()
{
int n;
cin >> n;
v.resize (100001);
for (int i = 1; i <= n; i++)
cin >> v[i];
int nr_teste;
cin >> nr_teste;
while (nr_teste--)
{
cin >> cerinta >> x;
int result1 = binSrch01 (1, n, x);
int result2 = binSrch2 (1, n, x);
if (cerinta == 0)
{
if (v[result1] == x)
cout << result1 << "\n";
else
cout << "-1" << "\n";
}
else if (cerinta == 1)
cout << result1 << "\n";
else
cout << result2 << "\n";
}
return 0;
}