Cod sursa(job #905834)

Utilizator SPDionisSpinei Dionis SPDionis Data 6 martie 2013 11:02:49
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <iostream>

using std::cout;
using std::vector;
using std::lower_bound;
using std::upper_bound;
std::ifstream in("cautbin.in");
std::ofstream out("cautbin.out");
vector<int> a;
int N,M;


int main()
{
    in >> N;
    for (int i = 0; i < N; ++i) {
        int x; in >> x;
        a.push_back(x);
    }

    in >> M;
    for (int i = 0; i < M; ++i)
    {
        int x,y; in >> x >> y;
        if ( x == 0 ) {
            int p = lower_bound(a.begin(), a.end(), y + 1) - a.begin();
            if ( a[p - 1] == y ) out << p << '\n';
            else out << -1 << '\n';
        } else if ( x == 1 ) {
            int p = lower_bound(a.begin(), a.end(), y + 1) - a.begin();
            out << p << '\n';
        } else if ( x == 2 ) {
            int p = upper_bound(a.begin(), a.end(), y - 1) - a.begin() + 1;
            out << p << '\n';
        }
    }


    in.close();
    out.close();
    return 0;
}