Pagini recente » Cod sursa (job #3283629) | Rating Petrusca Sergiu (SergiuPetrusca) | Cod sursa (job #1935072) | Cod sursa (job #3250210) | Cod sursa (job #811259)
Cod sursa(job #811259)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int N, M;
vector <int> V;
void read() {
fin >> N;
for(int i = 0; i < N; i++) {
int x;
fin >> x;
V.push_back(x);
}
}
int cbin1(int type, int val) {
int lo = 0, hi = N-1;
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
if (V[mid] > val) {
hi = mid;
} else {
lo = mid;
}
}
if (type == 0 && V[lo] != val) {
return -1;
}
return lo + 1;
}
int cbin2(int type, int val) {
int lo = 0, hi = N-1;
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
if (V[mid] < val) {
lo = mid;
} else {
hi = mid;
}
}
return hi + 1;
}
int main() {
read();
fin >> M;
while (M--) {
int type, val;
fin >> type >> val;
if (type == 0 || type == 1) {
fout << cbin1(type, val) << "\n";
} else {
fout << cbin2(type, val) << "\n";
}
}
return 0;
}