Cod sursa(job #2445559)

Utilizator ArkhamKnightyMarco Vraja ArkhamKnighty Data 4 august 2019 17:11:45
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

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

int n, m;
vector<int> v;

void citire()
{
    int x;
    cin >> n;
    for(int i = 1 ; i <= n ; i++)
        cin >> x, v.push_back(x);
    sort(v.begin(), v.end());
    cin >> m;
}

void rez()
{
    int x, y;

    for(int i = 1 ; i <= m ; i++)
    {
        cin >> x >> y;
        if(!x)
        {
            int lw = upper_bound(v.begin(), v.end(), y) - v.begin();
            (v[lw - 1] == y && lw != 0) ? cout << lw << '\n' : cout << -1 << '\n';
        }
        else if(x == 1)
            cout << upper_bound(v.begin(), v.end(), y) - v.begin() << '\n';
        else if(x == 2)
            cout << lower_bound(v.begin(), v.end(), y) - v.begin() + 1 << '\n';
    }
}

int main()
{
    citire();
    rez();

    return 0;
}