Cod sursa(job #2643498)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 19 august 2020 23:34:58
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");

int V[100001];

int main () {
  int N, M, x, y, t;
  fin >> N;
  for (int i = 1; i <= N; ++i)
    fin >> V[i];
  sort (V + 1, V + N + 1);
  fin >> M;
  for (int i = 1; i <= M; ++i) {
    fin >> t >> y;
    if (t == 0) {
      x = upper_bound (V + 1, V + N + 1, y) - V - 1;
      if (x <= N && x > 0 && V[x] == y)
        fout << x << '\n';
      else
        fout << "-1\n";
    }
    else
      if (t == 1) {
        x = lower_bound (V + 1, V + N + 1, y + 1) - V - 1;
        fout << x << '\n';
      }
    else {
      x = upper_bound (V + 1, V + N + 1, y - 1) - V;
      fout << x << '\n';
    }
  }
}