Cod sursa(job #2646585)

Utilizator DormeoNoapte Buna Dormeo Data 1 septembrie 2020 15:06:12
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 100000 + 5;

int a[DIM];

int main()
{
    int n, m;

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

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