Cod sursa(job #1199286)
Utilizator | Data | 18 iunie 2014 19:14:22 | |
---|---|---|---|
Problema | Cautare binara | Scor | 10 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.9 kb |
#include <fstream>
#include <algorithm>
using namespace std;
int v[100000];
int main()
{
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int N, M, query, x;
f >> N;
for(int i=1; i<=N; i++)
f >> v[i];
f >> M;
for(int i=1; i<=M; i++) {
f >> query >> x;
if (query == 0) {
int y = upper_bound(v+1,(v+1)+N,x)-(v+1);
if(v[y-1]==x && y<N)
g << y << "\n";
else
g << -1 << "\n";
}
if (query == 1) {
g << lower_bound(v+1,(v+1)+N,x+1)-(v+1) << "\n";
}
if (query == 2) {
g << upper_bound(v+1,(v+1)+N,x-1)-(v+1)+1 << "\n";
}
}
return 0;
}