Cod sursa(job #2640853)

Utilizator liviu2000Dragomirescu Liviu liviu2000 Data 8 august 2020 16:57:27
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("cautbin.in") ;
ofstream fout("cautbin.out") ;

int main()
{
    int n , i , x , q , tp ,poz;
    vector<int> v;
    fin >> n ;
    for ( i = 1 ; i <= n ; i++ )
    {
        fin >> x ;
        v.push_back(x) ;
    }
    fin >> q ;
    while ( q-- )
    {
        fin >> tp >> x ;
        if ( tp == 0 )
        {
            poz = upper_bound(v.begin(),v.end(),x) - v.begin() ;
            if ( v[poz-1] == x )
                fout << poz << '\n' ;
            else
                fout << -1 << '\n' ;
        }
        else if ( tp == 1 )
        {
            fout << upper_bound(v.begin(),v.end(),x) - v.begin() << '\n';
        }
        else
        {
            fout << lower_bound(v.begin(),v.end(),x) - v.begin()+1 << '\n';
        }
    }
}