Pagini recente » Rating Gheorghe Tudor (reksa) | Links | Rating Bratu Alexandru (alex210046) | Profil claudiu_sincai | Cod sursa (job #811252)
Cod sursa(job #811252)
#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);
}
}
void cbin1(int type, int val) {
int lo = -1, hi = N;
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
if (V[mid] > val) {
hi = mid;
} else {
lo = mid;
}
}
if (type == 0 && V[lo] != val) {
fout << "-1\n";
} else {
fout << lo + 1 << "\n";
}
}
void cbin2(int type, int val) {
int lo = -1, hi = N;
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
if (V[mid] < val) {
lo = mid;
} else {
hi = mid;
}
}
if (type == 0 && V[hi] != val) {
fout << "-1\n";
} else {
fout << hi + 1 << "\n";
}
}
int main() {
read();
fin >> M;
while (M--) {
int type, val;
fin >> type >> val;
if (type == 0 || type == 1) {
cbin1(type, val);
} else {
cbin2(type, val);
}
}
return 0;
}