Cod sursa(job #2303675)
| Utilizator | Data | 16 decembrie 2018 18:48:06 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include <bits/stdc++.h>
using namespace std;
int n, x, y, m, v[100011];
int main()
{
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
fin >> n;
for (int i=1;i<=n;i++) {
fin >> v[i];
}
fin >> m;
for (int i=1;i<=m;i++) {
fin >> x >> y;
int c = binary_search(v+1,v+n+1,y);
if (x!=2) {
if (c==0 && x==0) fout << -1 << '\n';
else if (c==1) fout << (upper_bound(v+1,v+n+1,y) - v)-1;
else fout << (lower_bound(v+1,v+n+1,y) - v);
}
else {
fout << lower_bound(v+1,v+n+1,y)-v;
}
}
return 0;
}
