Cod sursa(job #3212498)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 11 martie 2024 20:13:18
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x << ": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "cautbin";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

int n;

int a[100005];

int main()
{

    fin >> n;
    for (int i = 1; i <= n; ++i)
        fin >> a[i];
    int m;
    fin >> m;
    while (m--)
    {
        int op, x;
        fin >> op >> x;
        if (op == 0)
            fout << (a[upper_bound(a + 1, a + n + 1, x) - a - 1] == x ? upper_bound(a + 1, a + n + 1, x) - a - 1 : -1) << '\n';
        else if (op == 1)
            fout << upper_bound(a + 1, a + n + 1, x) - a - 1 << '\n';
        else
            fout << lower_bound(a + 1, a + n + 1, x) - a << '\n';
    }
    return 0;
}