Cod sursa(job #2918271)

Utilizator Redstoneboss2Fabian Lucian Redstoneboss2 Data 10 august 2022 19:33:30
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m;
vector<int> v;

int main(){

    fin >> n;

    v.resize(n);

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

    sort(v.begin(), v.end());

    fin >> m;

    for(int i = 0, c, temp; i < m; i++){
        fin >> c >> temp;

        if(c == 0){
            auto x = upper_bound(v.begin(), v.end(), temp);
                
            if(*(x-1) == temp){
                fout << x - v.begin() << '\n';
            }else{
                fout << "-1\n";
            }

        }else if(c == 1){
            fout << upper_bound(v.begin(), v.end(), temp) - v.begin() << '\n';
        }else{
            fout << (lower_bound(v.begin(), v.end(), temp) - v.begin()) + 1 << '\n';
        }
    }


    return 0;
}