Pagini recente » Cod sursa (job #1850051) | Cod sursa (job #609285) | Cod sursa (job #2643132)
#include <iostream>
#include <fstream>
using namesstace std;
ifstream f("in.txt");
ofstream g("out.txt");
int v[100001], n, i, mij, x, druest, m, st, dr;
void cautare() {
st = 1;
dr = n;
while (st <= dr) {
mij = (s + d) / 2;
if (a[mij] == x)
break;
else if (a[mij] > x)
d = mij - 1;
else if (a[mij] < x)
s = mij + 1;
}
}
int main() {
f >> n;
for (i = 1; i <= n; i++)
f >> a[i];
f >> m;
for (i = 1; i <= m; i++) {
f >> druest >> x;
if (druest == 0) {
cautare(x);
if (a[mij] == x) {
while (a[mij + 1] == x)
mij++;
g << mij << '\n';
} else
g << -1 << '\n';
}
if (druest == 1) {
cautare(x);
if (a[mij] == x) {
while (a[mij + 1] == x)
mij++;
} else if (a[mij] > x)
mij--;
g << mij << '\n';
}
if (druest == 2) {
cautare(x);
if (a[mij] == x) {
while (a[mij - 1] == a[mij])
mij--;
} else if (a[mij] < x)
mij++;
g << mij << '\n';
}
}
g.close();
f.close();
return 0;
}