Cod sursa(job #1961445)

Utilizator cristicretancristi cretan cristicretan Data 11 aprilie 2017 09:36:09
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>
#define NMax 100001
using namespace std;

ifstream f("cautbin.in");
ofstream g("cautbin.out");

int n, m, t, x, y;
int v[NMax];

int main()
{
    f >> n;
    for(int i = 1; i <= n; ++i)
        f >> v[i];

    sort(v + 1, v + 1 + n);

    f >> m;
    for(int i = 1; i <= m; ++i)
    {
        f >> t >> y;
        if(t == 0)
        {
            x = upper_bound(v + 1, v + 1 + n, y) - v - 1;
            if(x >= 1 && x <= n && v[x] == y) g << x << '\n';
            else g << "-1" << '\n';
        }
        else if(t == 1)
        {
            x = lower_bound(v + 1, v + 1 + n, y + 1) - v - 1;
            g << x << '\n';
        }
        else
        {
            x = upper_bound(v + 1, v + 1 + n, y - 1) - v;
            g << x << '\n';
        }
    }
    return 0;
}