Cod sursa(job #2601375)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 14 aprilie 2020 13:21:34
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int V[100100];
int main ()
{
    int N, M, x, y, i, t;
    f>>N;
    for(i=1;i<=N;++i) f>>V[i];
    sort(V+1,V+N+1);
    f>>M;
    for (i = 1; i <= M; ++ i)
    {
        f>>t>>y;
        if (t == 0)
        {
            x = upper_bound(V + 1, V + N + 1, y) - V - 1;
            if (x <= N && x >= 1 && V[x] == y) g<<x<<'\n';
            else g<<-1<<'\n';
        }
        else if (t == 1)
        {
            x = lower_bound(V + 1, V + N + 1, y + 1) - V - 1;
            g<<x<<'\n';
        } else
        {
            x = upper_bound(V + 1, V + N + 1, y - 1) - V;
            g<<x<<'\n';
        }
    }
    f.close();
    g.close();
    return 0;
}