Cod sursa(job #2500134)

Utilizator bpalaniciPalanici Bogdan bpalanici Data 27 noiembrie 2019 11:53:27
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.39 kb
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <stack>
#include <unordered_map>
#include <utility>
#include <queue>
#include <vector>

#define sz(x) (ll)((x).size())
#define mp make_pair
#define pb push_back
#define ff first
#define ss second

using namespace std;

typedef unsigned long long ull;
typedef long long ll ;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <long long, long long> pll;

template <class T>
const bool isImpar(const T &x) {return (bool) (x & 1);}

int n, t, v[200005];


int cbin0(int val)
{
   int st = 1;
   int dr = n;
   int ans = -1;
   int mij;

   while(st <= dr)
   {
      mij = st + (dr-st)/2;
      if(v[mij] == val)
      {
         ans = mij;
         st = mij+1;
      }
      else if(v[mij] > val)
      {
         dr = mij-1;
      }
      else
      {
         st = mij+1;
      }
   }
   return ans;

}
int cbin1(int val)
{
   int st = 1;
   int dr = n;
   int ans = 0;
   int mij;

   while(st <= dr)
   {
      mij = st + (dr-st)/2;
      if(v[mij] == val)
      {
         ans = mij;
         st = mij+1;
      }
      else if(v[mij] > val)
      {
         dr = mij-1;
      }
      else
      {
         ans = mij;
         st = mij+1;
      }
   }
   return ans;

}
int cbin2(int val)
{
   int st = 1;
   int dr = n;
   int ans = 0;
   int mij;

   while(st <= dr)
   {
      mij = st + (dr-st)/2;
      if(v[mij] == val)
      {
         ans = mij;
         dr = mij-1;
      }
      else if(v[mij] > val)
      {
         ans = mij;
         dr = mij-1;

      }
      else
      {
         st = mij+1;
      }
   }
   return ans;

}

int main() {
    //ios_base::sync_with_stdio(false);
    //cin.tie(0);
    freopen("cautbin.in", "r", stdin);
    freopen("cautbin.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> v[i];
    sort(v + 1, v + n + 1);
    cin >> t;
    for (int i = 1, x, y; i <= t; i++) {
        cin >> x >> y;
        if (x == 0)
            cout << cbin0(y);
        if (x == 1)
            cout << cbin2(y);
        if (x == 2)
            cout << cbin1(y);
        cout << "\n";
    }
    return 0;
}