Cod sursa(job #3136968)

Utilizator sebuxSebastian sebux Data 9 iunie 2023 17:20:35
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>
#define optim ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define let auto
#define popcount __builtin_popcount
#define ctzll __builtin_ctzll
#define clzll __builtin_clzll

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




const int sze = 1e5;
int v[sze + 1];
int n;


int main()
{
    fin>>n;
    for(int i = 1;i<=n;++i) fin>>v[i];
    int m;
    fin>>m;
    int op, a, x;
    while(m--){
        fin>>op>>a;
        switch (op) {
            case 0:
            x = upper_bound(v + 1, v + n + 1, a) - v - 1;
            if(v[x] == a && x >= 1 && x <=n){
                fout<<x<<'\n';
            }
            else fout<<"-1\n";
            break;
            case 1:
            fout<<lower_bound(v + 1, v + n + 1, a + 1) - v - 1<<'\n';
            break;
            case 2:
            fout<<upper_bound(v + 1, v + n + 1, a - 1) - v<<'\n';
            break;
        }


    }






    return 0;
}