Cod sursa(job #2600679)

Utilizator avtobusAvtobus avtobus Data 13 aprilie 2020 08:51:10
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
// binary search using functions defined in STL
#include <stdio.h>
#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 0x3f3f3f3f;

ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
const int Nmax = 100555;
int N, M, a[Nmax];

int main(void) {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  fin >> N;
  rep(i, N) { fin >> a[i]; }
  fin >> M;
  int q, x;
  int *it;
  rep(i, M) {
    fin >> q >> x;
    switch(q) {
      case 0:
        it = upper_bound(a, a+N, x);
        --it;
        if (*it == x) {
          fout << (it - a) + 1;
        } else {
          fout << -1;
        }
        break;
      case 1:
        it = upper_bound(a, a+N, x);
        --it;
        fout << (it - a) + 1;
        break;
      case 2:
        it = lower_bound(a, a+N, x);
        fout << (it - a) + 1;
        break;
    }
    fout << '\n';
  }


  return 0;
}