Pagini recente » Cod sursa (job #2904006) | Cod sursa (job #163366) | Cod sursa (job #2458618) | Cod sursa (job #1514557) | Cod sursa (job #2725660)
#include <fstream>
#include <vector>
using namespace std;
int LastNotGreater(const vector<int>& vec, int value) {
auto pos = -1;
auto pow = 1 << 25;
while (pow > 0) {
auto next = pos + pow;
pow >>= 1;
if (next < (int)vec.size() && vec[next] <= value) {
pos = next;
}
}
return pos;
}
int FindExact(const vector<int>& vec, int value) {
auto pos = LastNotGreater(vec, value);
return (pos > 0 && vec[pos] == value) ? pos : -1;
}
int FirstNotLess(const vector<int>& vec, int value) {
auto pos = LastNotGreater(vec, value - 1) + 1;
return (pos < (int)vec.size()) ? pos : -1;
}
int main() {
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n;
fin >> n;
vector<int> vec(n);
for (auto& num : vec) {
fin >> num;
}
int queries;
fin >> queries;
for (auto i = 0; i < queries; i += 1) {
int type, value;
fin >> type >> value;
auto res = (type == 0) ? FindExact(vec, value)
: (type == 1) ? LastNotGreater(vec, value)
: FirstNotLess(vec, value);
fout << (res == -1 ? -1 : (res + 1)) << "\n";
}
return 0;
}