Cod sursa(job #3360718)

Utilizator PetruVPetru Varabiescu PetruV Data 16 iulie 2026 10:06:10
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <bits/stdc++.h>

using namespace std;

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

const int MAXN = 100000;

int n;
int v[MAXN + 1];

int c0(int x) {
    int st, dr, mij;

    st = 1;
    dr = n + 1;

    while (dr - st > 1) {
        mij = st + (dr - st) / 2;

        if (v[mij] <= x)
            st = mij;
        else
            dr = mij;
    }
    return (v[st] == x ? st : -1);
}

int c1(int x) {
    int st, dr, mij;

    st = 1;
    dr = n + 1;

    while (dr - st > 1) {
        mij = st + (dr - st) / 2;
        if (v[mij] <= x)
            st = mij;
        else
            dr = mij;
    }
    return st;
}

int c2(int x) {
    int st, dr, mij;

    st = 0;
    dr = n;

    while (dr - st > 1) {
        mij = st + (dr - st) / 2;
        if (v[mij] < x)
            st = mij;
        else
            dr = mij;
    }
    return dr;
}

int main() {

    int m, c, x;

    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> v[i];

    fin >> m;
    for (int i = 1; i <= m; i++) {
        fin >> c >> x;

        if (c == 0)
            fout << c0 (x) << '\n';
        else if (c == 1)
            fout << c1 (x) << '\n';
        else if (c == 2)
            fout << c2 (x) << '\n';
    }

    return 0;
}