Cod sursa(job #1675594)

Utilizator superstar1998Moldoveanu Vlad superstar1998 Data 5 aprilie 2016 13:43:33
Problema Cautare binara Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#define MAXN 100001
#define INFILE "cautbin.in"
#define OUTFILE "cautbin.out"
using namespace std;
ifstream f(INFILE);
ofstream g(OUTFILE);
int n,m,v[MAXN],op,x,p;
int main()
{
    f>>n;
    for(int i=1;i<=n;i++)
        f>>v[i];
    f>>m;
    for(;m--;)
    {
        f>>op>>x;
        if(!op)
        {
            p=upper_bound(v+1,v+n+1,x)-v-1;
            if(p>=1&&p<=n&&v[p]==x)g<<p;
            else g<<-1<<"\n";
        }
        else if(op==1)
        {
            p=lower_bound(v+1,v+n+1,x+1)-v-1;
            g<<p<<"\n";
        }
        else
        {
            p=upper_bound(v+1,v+n+1,x-1)-v;
            g<<p<<"\n";
        }
    }
    f.close();
    g.close();
    return 0;
}