Cod sursa(job #1819729)

Utilizator facelessIonut Balauca faceless Data 30 noiembrie 2016 19:29:59
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int a[100100],x,y,m,n,i;
int main()
{
    fin>>n;
    for(i=1;i<=n;i++) fin>>a[i];
    fin>>m;
    while(m--)
    {
        fin>>y>>x;
        if(y==0)
        {
            y=upper_bound(a+1,a+n+1,x)-(a+1);
            if(a[y]==x && y!=1) fout<<y<<"\n";
                 else fout<<"-1\n";
        }
        else
            {
             if(y==1) fout<<upper_bound(a+1,a+n+1,x)-(a+1)<<"\n";
              else fout<<lower_bound(a+1,a+n+1,x)-a<<"\n";
            }
    }
}