Cod sursa(job #2314780)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 9 ianuarie 2019 01:17:57
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#include <algorithm>

#define NMAX 100010

using std::lower_bound;
using std::upper_bound;

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

int n, m;
int v[NMAX];

int main() {
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> v[i];

    fin >> m;
    for (int i = 0; i < m; i++) {
        int x, y;
        fin >> x >> y;

        if (!x) {
            int pos = upper_bound(v + 1, v + n + 1, y) - v - 1;
            if (1 <= pos && pos <= n && v[pos] == y)
                fout << pos << '\n';
            else
                fout << "-1\n";
        }
        else if (x == 1)
            fout << lower_bound(v + 1, v + n + 1, y + 1) - v - 1 << '\n';
        else
            fout << upper_bound(v + 1, v + n + 1, y - 1) - v << '\n';
    }

    fout.close();
    return 0;
}