Cod sursa(job #2270780)
| Utilizator | Data | 27 octombrie 2018 15:42:51 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 40 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.63 kb |
#include <bits/stdc++.h>
using namespace std;
int n, i, m, a, x, v[100011], b, c, d;
int main()
{
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
fin >> n;
for (i=1;i<=n;i++) fin >> v[i];
fin >> m;
for (i=1;i<=m;i++) {
fin >> a >> x;
b = lower_bound(v+1,v+n+1,x) - v;
c = upper_bound(v+1,v+n+1,x) - v;
if (a==0) {
if (v[c-1]==x) fout << c-1 << endl;
else fout << -1 << endl;
}
else if (a==1) {
fout << c-1 << endl;
}
else {
fout << b << endl;
}
}
return 0;
}
