Cod sursa(job #2969968)

Utilizator sandry24Grosu Alexandru sandry24 Data 23 ianuarie 2023 22:19:19
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second
 
void solve(){
    int n;
    cin >> n;
    vi a(n);
    for(int i = 0; i < n; i++)
        cin >> a[i];
    int m;
    cin >> m;
    for(int i = 0; i < m; i++){
        int x, b;
        cin >> x >> b;
        if(x == 0){
            auto h = upper_bound(a.begin(), a.end(), b);
            --h;
            if(*h == b)
                cout << h - a.begin() + 1 << '\n';
            else
                cout << -1 << '\n';
        } else if(x == 1){
            auto h = upper_bound(a.begin(), a.end(), b);
            --h;
            cout << h - a.begin() + 1 << '\n';
        } else {
            auto h = lower_bound(a.begin(), a.end(), b);
            cout << h - a.begin() + 1 << '\n';
        }
    }
}  
 
int main(){
    freopen("cautbin.in", "r", stdin);
    freopen("cautbin.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}