Cod sursa(job #2277703)

Utilizator ALEx6430Alecs Andru ALEx6430 Data 6 noiembrie 2018 19:00:31
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>
using namespace std;

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

int main()
{
    ios_base::sync_with_stdio(false);
    in.tie(NULL);

    int n;
    in >> n;

    vector<int> v(n);
    int it;

    for(int i = 0; i < n; i++) in >> v[i];

    int m;
    in >> m;

    while(m--)
    {
        short q;
        int x;

        in >> q >> x;

        if(q == 0)
        {
            it = upper_bound(v.begin(), v.end(), x) - v.begin()-1;
            if(it >= 0 && it < n && v[it] == x) out << it+1;
            else out << -1;
        }
        else if(q == 1)
        {
            it = lower_bound(v.begin(), v.end(), x+1) - v.begin();
            out << it;
        }
        else
        {
            it = upper_bound(v.begin(), v.end(), x-1) - v.begin()+1;
            out << it;
        }

        out << '\n';
    }

    return 0;
}