Cod sursa(job #2263693)

Utilizator SqueekDanielTodasca Daniel SqueekDaniel Data 19 octombrie 2018 01:23:20
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>

#define MaxN 100005

std::ifstream InFile("cautbin.in");
std::ofstream OutFile("cautbin.out");

int N, Q, V[MaxN];

int main()
{
    InFile >> N;
    for (int i=0; i<N; ++i)
        InFile >> V[i+1];

    InFile >> Q;

    int Type, x, p;
    while (Q--) {
        InFile >> Type >> x;

        if (Type == 0) {
            p = std::upper_bound(V+1, V+N+1, x) - V-1;
            if (V[p] != x) p = -1;
        } else
        if (Type == 1) {
            p = std::upper_bound(V+1, V+N+1, x) - V-1;
        } else
        if (Type == 2) {
            p = std::lower_bound(V+1, V+N+1, x) - V;
        }

        OutFile << p << '\n';
    }

  return 0;
}