Cod sursa(job #2723430)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 14 martie 2021 00:54:05
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

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

void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

const int N = 1e5 + 5;

int a[N];

int main()
{
    usain_bolt();

    int n, q;

    fin >> n;
    for(int i = 1; i <= n; ++i) {
        fin >> a[i];
    }
    fin >> q;
    for(; q; --q) {
        int type, x;

        fin >> type >> x;
        if(type == 0) {
            int pos = upper_bound(a + 1, a + 1 + n, x) - a - 1;
            if(pos >= 1 && a[pos] == x) {
                fout << pos << "\n";
            }
            else {
                fout << -1 << "\n";
            }
        }
        else if(type == 1) {
            int pos = upper_bound(a + 1, a + 1 + n, x) - a - 1;
            fout << pos << "\n";
        }
        else if(type == 2) {
            int pos = lower_bound(a + 1, a + 1 + n, x) - a;
            fout << pos << "\n";
        }
    }
    return 0;
}