Cod sursa(job #2137399)

Utilizator edykpStoica Eduard-Constantin edykp Data 20 februarie 2018 19:31:28
Problema Cautare binara Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include<algorithm>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n,v[100001];
int main()
{
    int x;
  f>>n;
  for(int i=1;i<=n;i++)
    f>>v[i];
    int m;
    f>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        f>>x>>y;
        if(x==0) {int z=upper_bound(v+1,v+n+1,y)-v-1; if(v[z]==y) g<<z<<endl; else g<<-1<<endl;}
        if(x==1) {int z=upper_bound(v+1,v+n+1,y)-v-1;if(v[z]==y) g<<z<<endl; else g<<lower_bound(v+1,v+n+1,y)-v-1<<endl;}
        if(x==2) {int z=lower_bound(v+1,v+n+1,y)-v; if(v[z]==y) g<<z<<endl; else g<<upper_bound(v+1,v+n+1,y)-v<<endl;}
    }
    return 0;
}