Cod sursa(job #2485760)

Utilizator dragos99Homner Dragos dragos99 Data 1 noiembrie 2019 23:06:18
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include<bits/stdc++.h>

using namespace std;
    ifstream f("cautbin.in");
    ofstream g("cautbin.out");

int n, m;
vector<int> v;
int main()
{
    f>>n;
    for(int i = 0 ; i < n ; i++){
        int x;
        f>>x;
        v.push_back(x);
    }
    f>>m;
    for(int i = 0 ; i < m ; i++){
        int question, x, y;
        f>>question>>x;
        if(question == 0){
            y = upper_bound(v.begin(), v.end(), x) - v.begin() - 1;
            if(v[y] == x && y >= 0 && y <= n){
                g<<y + 1<<'\n';
            }
            else{
                g<<-1<<'\n';
            }
        }
        else if(question == 1){
            y = lower_bound(v.begin(), v.end(), y + 1) - v.begin() - 1;
            g<<y + 1<<'\n';
        }
        else {
            y = upper_bound(v.begin(), v.end(), y - 1) - v.begin();
            g<<y + 1<<'\n';
        }
    }

    return 0;
}