Cod sursa(job #1922508)

Utilizator KanghuAndre Popescu Kanghu Data 10 martie 2017 17:44:34
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int N, Q;
vector<int> V;

int main()
{
    ifstream i("cautbin.in");
    ofstream o("cautbin.out");

    i >> N;

    for(int a = 0; a < N; a++)
    {
        int x;
        i >> x;

        V.push_back(x);
    }

    sort(V.begin(), V.end());
    i >> Q;

    for(int a = 0; a < Q; a++)
    {
        std::vector<int>::iterator k;
        int x, y;
        i >> x >> y;

        if(x == 0)
        {
            k = upper_bound(V.begin(), V.end(), y);

            if(*(k - 1) == y)
                cout << k - V.begin() << '\n';
            else
                cout << "-1" << '\n';
        }

        else if(x == 1)
        {
            k = upper_bound(V.begin(), V.end(), y);

            cout << k - V.begin() << '\n';
        }

        else
        {
            k = lower_bound(V.begin(), V.end(), y);

            cout << k - V.begin() + 1 << '\n';
        }
    }


    return 0;
}