Pagini recente » Cod sursa (job #2516971) | Cod sursa (job #155897) | Cod sursa (job #987373) | Cod sursa (job #2089019) | Cod sursa (job #1122171)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f ("cautbin.in");
ofstream g ("cautbin.out");
int n;
int v[100001];
void citeste () {
f >> n;
for (int i = 1; i <= n; i++) f >> v[i];
}
int query (int q, int valoare) {
if (q == 0) {
int x = upper_bound (v + 1, v + n + 1, valoare) - v - 1;
if (x >= 1 && x <=n && v[x] == valoare)
return x;
return -1;
}
else if (q == 1) {
int x = lower_bound (v + 1, v + n + 1, valoare + 1) - v - 1;
return x;
}
int x = upper_bound (v + 1, v + n + 1, valoare - 1) - v;
return x;
}
void rezolva () {
int m, q, x;
f >> m;
for (int i = 1; i <= m; i++) {
f >> q >> x;
g << query (q, x) << '\n';
}
}
int main () {
citeste ();
rezolva ();
return 0;
}