Cod sursa(job #2817066)

Utilizator ElizaTElla Rose ElizaT Data 12 decembrie 2021 19:22:13
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 1e5;
int v[NMAX + 5];
int poz,pas,a,n;
bool ok(int cer, int poz, int val) {
    if (poz > n)
        return 0;
    if (cer == 2)
        return v[poz] < val;
    return v[poz] <= val;
}
int cb(int cer, int val) {
    poz = 0;
    pas = a;
    while (pas) {
        if (ok(cer, pas + poz, val))
            poz += pas;
        pas >>= 1;
    }
    if (cer == 0 && v[poz] != val)
        return -1;
    if (cer == 2)
        poz++;
    return poz;
}
int main()
{
    ifstream fin("cautbin.in");
    ofstream fout("cautbin.out");
    int m,cer,x;
    fin >> n;
    for (a = 1;a < n;a <<= 1);
    for (int i = 1;i <= n;i++)
        fin >> v[i];
    fin >> m;
    for (int i = 0;i < m;i++) {
        fin >> cer >> x;
        fout << cb(cer, x) << '\n';
    }
    return 0;
}