Cod sursa(job #2270775)
| Utilizator | Data | 27 octombrie 2018 15:28:09 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.67 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=0;i<n;i++) fin >> v[i];
fin >> m;
for (i=1;i<=m;i++) {
fin >> a >> x;
b = lower_bound(v,v+n,x) - v;
c = upper_bound(v,v+n,x) - v;
if (a==0) {
if (v[c-1]==x) fout << c << endl;
else fout << -1 << endl;
}
else if (a==1) {
fout << c << endl;
}
else {
if (v[b+1]==x) fout << b+1 << endl;
else fout << b+2 << endl;
}
}
return 0;
}
