Cod sursa(job #1425580)

Utilizator IoanaaChriistinaaPascal Ioana-Cristina IoanaaChriistinaa Data 27 aprilie 2015 18:45:51
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>
#include <algorithm>
using namespace std;
 
ifstream f("cautbin.in");
ofstream g("cautbin.out");
 
int N, M;
vector<int> v;
 
int main()  {
    f >> N;
    v.resize(N);
    for (int &i : v)
        f >> i;
    f >> M;
    while (M--) {
        int t, val;
        f >> t >> val;
        if (t == 0) {
            auto it = upper_bound(v.begin(), v.end(), val);
            if (it == v.begin() || *(--it) != val)
                g << "-1\n";
            else
                g << it - v.begin() + 1 << "\n";
        } else if (t == 1) {
            g << upper_bound(v.begin(), v.end(), val) - v.begin() << "\n";
        } else {
            g << lower_bound(v.begin(), v.end(), val) - v.begin() + 1 << "\n";
        }
    }
    return 0;
}