Cod sursa(job #2244531)

Utilizator cosmin.pascaruPascaru Cosmin cosmin.pascaru Data 22 septembrie 2018 23:23:37
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <vector>
#include <stack>
#include <set>
#include <assert.h>
#include <queue>
#include <chrono>
#include <memory>

using LL = long long;
using ULL = int long long;

const std::string _problemName = "cautbin";

namespace std {
std::ifstream fin(_problemName + ".in");
std::ofstream fout(_problemName + ".out");
}

 #define USE_FILES

#ifdef USE_FILES
#define cin fin
#define cout fout
#endif

int main() {

    int n;
    std::cin >> n;
    
    std::vector<int> arr(n);

    for (int idx = 0; idx < n; ++idx) {
        std::cin >> arr[idx];
    }

    int m;
    std::cin >> m;

    while (m--) {
        int op, x;
        std::cin >> op >> x;

        if (op == 0) {
            auto it = std::lower_bound(arr.rbegin(), arr.rend(), x, std::greater<int>());

            if (it != arr.rend() && *it == x) {
                std::cout << std::distance(arr.begin(), it.base()) << '\n';
            }
            else {
                std::cout << "-1\n";
            }
        }
        else if (op == 1) {
            auto it = std::lower_bound(arr.rbegin(), arr.rend(), x, std::greater<int>());
            std::cout << std::distance(arr.begin(), it.base()) << '\n';
        }
        else {
            auto it = std::lower_bound(arr.begin(), arr.end(), x);
            std::cout << std::distance(arr.begin(), it) << '\n';
        }
    }

    return 0;
}