Cod sursa(job #3241010)

Utilizator Tudor.1234Holota Tudor Matei Tudor.1234 Data 24 august 2024 19:47:06
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include "bits/stdc++.h"
const int NMAX = 100000;
int v[NMAX + 5], n, q;
inline static void Q0(){
           int x;
           std :: cin >> x;

                  int idx = std :: upper_bound(v + 1, v + n + 1, x) - v - 1;
                  if(idx >= 1 and v[idx] == x and idx <= n){
                        std :: cout << idx << '\n';
                  }else{
                         std :: cout << -1 << '\n';
                  }

}
inline static void Q1(){
            int x;
            std :: cin >> x;
             int idx =  std :: lower_bound(v + 1, v + n + 1, x + 1) - v - 1;
             std :: cout << idx << '\n';

}
inline static void Q2(){
            int x;
            std  :: cin >> x;
            std ::  cout << std :: upper_bound(v + 1, v + n + 1, x - 1) - v << '\n';
}
inline static void Solve(){
             std :: cin >> n;
             for(int i = 1; i <= n; i++){
                       std :: cin >> v[i];
             }
             std :: cin >> q;
             while(q --){
                   int op;
                   std :: cin >> op;
                   if(op == 0){
                         Q0();
                   }
                   if(op == 1){
                          Q1();
                   }
                   if(op == 2){
                         Q2();
                   }
             }
}
signed main(){
      freopen("cautbin.in","r",stdin);
      freopen("cautbin.out","w",stdout);
      std :: ios_base :: sync_with_stdio(false);
      std :: cin.tie(0);
      std :: cout.tie(0);
      Solve();
      return 0;
}