Pagini recente » Cod sursa (job #562079) | Cod sursa (job #437051) | Cod sursa (job #2940114) | Cod sursa (job #2672944) | Cod sursa (job #1721329)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
ifstream in("cautbin.in");
ofstream out("cautbin.out");
int n, m;
in >> n;
vector<int> v;
int xmin = 0, xmax = 0;
for (int i = 0; i < n; i++) {
int nr;
in >> nr;
v.push_back(nr);
if (xmin > nr) xmin = nr;
if (xmax < nr) xmax = nr;
}
//sort(v + 1, v.end());
in >> m;
for (int i = 0; i < m; i++) {
int intg, x;
in >> intg >> x;
if (intg == 0) {
vector<int>::iterator up = upper_bound(v.begin(), v.end(), x);
if (up - v.begin() > 0 && up - v.begin() < n)
cout << up - v.begin() << endl;
else
cout << "-1" << endl;
} else if (intg == 1) {
vector<int>::iterator up = upper_bound(v.begin(), v.end(), x + 1);
cout << up - v.begin() << endl;
} else if (intg == 2) {
vector<int>::iterator low = lower_bound(v.begin(), v.end(), x - 1);
cout << low - v.begin() + 1 << endl;
}
}
in.close();
out.close();
return 0;
}