Pagini recente » Cod sursa (job #295462) | Cod sursa (job #1730219) | Cod sursa (job #247747) | Cod sursa (job #1962962) | Cod sursa (job #1463902)
#include <fstream>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int v[100001], stanga, dreapta, i, N, M, x, t;
int main() {
int mijloc;
fin >> N;
for (i = 1; i <= N; i++) {
fin >> v[i];
}
fin >> M;
for (i = 1; i <= M; i++) {
fin >> t >> x;
if (t == 0) {
stanga = 1;
dreapta = N;
while (stanga <= dreapta) {
mijloc = (stanga + dreapta) / 2;
if (v[mijloc] <= x) {
stanga = mijloc + 1;
}
else {
dreapta = stanga - 1;
}
}
if (v[dreapta] == x){
fout << dreapta;
}
else {
fout << -1;
}
fout << "\n";
}
if (t == 1) {
stanga = 1;
dreapta = N;
while (stanga <= dreapta) {
mijloc = (stanga + dreapta) / 2;
if (v[mijloc] <= x) {
stanga = mijloc + 1;
}
else {
dreapta = mijloc - 1;
}
}
fout << dreapta<< "\n";
}
if (t == 2) {
stanga = 1;
dreapta = N;
while (stanga <= dreapta) {
mijloc = (stanga + dreapta) / 2;
if (v[mijloc] < x) {
stanga = mijloc + 1;
}
else {
dreapta = mijloc - 1;
}
}
fout << stanga << "\n";
}
}
return 0;
}