Cod sursa(job #2500094)

Utilizator bpalaniciPalanici Bogdan bpalanici Data 27 noiembrie 2019 11:29:35
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.38 kb
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <stack>
#include <unordered_map>
#include <utility>
#include <queue>
#include <vector>

#define sz(x) (ll)((x).size())
#define mp make_pair
#define pb push_back
#define ff first
#define ss second

using namespace std;

typedef unsigned long long ull;
typedef long long ll ;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <long long, long long> pll;

template <class T>
const bool isImpar(const T &x) {return (bool) (x & 1);}

int n, t, v[200005];

int cbin1(int x) {
    int m, s = 0, d = n - 1, result = -1;
    while (s <= d) {
        m = (s + d) / 2;
        if (v[m] == x) {
            result = m;
            d = m - 1;
        }
        else if (v[m] <= x) {
            result = m;
            s = m + 1;
        }
        else {
            d = m - 1;
        }
    }
    return (result == -1) ? result : result + 1;
}

int cbin2(int x) {
    int m, s = 0, d = n - 1, result = -1;
    while (s <= d) {
        m = (s + d) / 2;
        if (v[m] == x) {
            result = m;
            s = m + 1;
        }
        else if (v[m] > x) {
            d = m - 1;
        }
        else {
            result = m;
            s = m + 1;
        }
    }
    return (result == -1) ? result : result + 1;
}

int cbin0(int x) {
    int m, s = 0, d = n - 1, result = -1;
    while (s <= d) {
        m = (s + d) / 2;
        if (v[m] == x) {
            result = m;
            s = m + 1;
        }
        else if (v[m] <= x) {
            s = m + 1;
        }
        else {
            d = m - 1;
        }
    }
    return (result == -1) ? result : result + 1;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    freopen("cautbin.in", "r", stdin);
    freopen("cautbin.out", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> v[i];
    sort(v, v + n);
    cin >> t;
    for (int i = 1, x, y; i <= t; i++) {
        cin >> x >> y;
        if (x == 0)
            cout << cbin0(y);
        if (x == 1)
            cout << cbin2(y);
        if (x == 2)
            cout << cbin1(y);
        cout << "\n";
    }
    return 0;
}