Cod sursa(job #1877983)

Utilizator medicinedoctoralexandru medicinedoctor Data 13 februarie 2017 20:13:30
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

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

vector <int> a;

void read()
{
    int n;
    cin >> n;
    a.resize(n);
    for (int i=0; i<a.size(); cin >> a[i++]);
}

main()
{
    read();
    int m;
    cin >> m;
    for (int x,y; m ; m--)
    {
        cin >> x >> y;
        if (x==0)
        {
             vector <int> :: iterator i=upper_bound(a.begin(), a.begin() + a.size() , y);
             if (i>=a.begin() && i<a.end()) cout << i-a.begin() << '\n'; else cout << -1 << '\n';
        }
        if (x==1) cout << lower_bound(a.begin(), a.begin() + a.size(), y + 1) - a.begin() << '\n';
        if (x==2) cout << upper_bound(a.begin(), a.begin() + a.size(), y - 1) - a.begin()+1 << '\n';
    }
}