Cod sursa(job #2259438)

Utilizator Victor_InczeVictor Incze Victor_Incze Data 13 octombrie 2018 12:33:33
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>

using namespace std;

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

vector <int> v;
int n, m, x, t, y;

int main()
{
    in >> n;
    for (int i=0; i<n; i++)
    {
        in >> x;
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    in >> m;
    for (int i=1; i<=m; i++)
    {
        in >> t >> x;
        if (t==0)
        {
            y=upper_bound(v.begin(),v.end(),x)-v.begin()-1;
            if (y<n && y>=0 && v[y]==x)
                out << y+1 << '\n';
            else
                out << -1 << '\n';
        }
        else
            if (t==1)
            {
                y=lower_bound(v.begin(),v.end(),x+1)-v.begin();
                out << y << '\n';
            }
            else
            {
                y=upper_bound(v.begin(),v.end(),x-1)-v.begin()+1;
                out << y << '\n';
            }
    }
    return 0;
}