Pagini recente » Cod sursa (job #162774) | Cod sursa (job #740878) | Cod sursa (job #2098649) | Cod sursa (job #1172546) | Cod sursa (job #1238607)
#include <iostream>
#include <fstream>
using namespace std;
#define MAXN 100005
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n;
int a[MAXN];
int caut0(int x)
{
int st = 1, dr = n;
while (st <= dr)
{
int mij = ((st + dr) >> 1);
if (a[mij] == x && a[mij] < a[mij + 1]) {
return mij;
}
if (a[mij] <= x) {
st = mij + 1;
} else if (a[mij] > x) {
dr = mij - 1;
}
}
return -1;
}
int caut1(int x)
{
int st = 1, dr = n;
while (st <= dr)
{
int mij = ((st + dr) >> 1);
if (a[mij] <= x && a[mij + 1] > x) {
return mij;
}
if (a[mij] <= x) {
st = mij + 1;
} else if (a[mij] > x) {
dr = mij - 1;
}
}
return -1;
}
int caut2(int x)
{
int st = 1, dr = n;
while (st <= dr)
{
int mij = ((st + dr) >> 1);
if (a[mij] >= x && a[mij - 1] < x) {
return mij;
}
if (a[mij] < x) {
st = mij + 1;
} else if (a[mij] >= x) {
dr = mij - 1;
}
}
return -1;
}
int main()
{
f >> n;
for (int i = 1; i <= n; i++)
{
f >> a[i];
}
a[n + 1] = a[n] + 1;
int m;
f >> m;
for (int i = 1; i <= m; i++)
{
int op, x;
f >> op >> x;
if (op == 0) g << caut0(x) << '\n';
if (op == 1) g << caut1(x) << '\n';
if (op == 2) g << caut2(x) << '\n';
}
f.close();
g.close();
return 0;
}