Advertisement
7oSkaaa

Binary Search

Aug 10th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1.   #include <bits/stdc++.h>
  2.   using namespace std;
  3.  
  4.   #define read(vec, n) for(int i = 0 ; i < n && cin >> vec[i] ; i++);
  5.   #define print(v, template) copy(all(v), ostream_iterator <template> (cout, " "));
  6.   #define read_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> nums[i][j]; j++);
  7.   #define loop(a, b, c) for(int i = a ; i < (b); i += c)
  8.   #define Ceil(n, m) ((n / m) + ( n % m ? 1 : 0))
  9.   #define all(vec) vec.begin(),vec.end()
  10.   #define rall(vec) vec.rbegin(),vec.rend()
  11.   #define Pair pair <int,int>
  12.   #define ll long long
  13.   #define ull unsigned long long
  14.   #define Mod 1'000'000'007
  15.   #define INF 2000'000'000
  16.   #define PI 3.14159
  17.  
  18.   void Code_Crush(){
  19.     ios_base::sync_with_stdio(false);   cin.tie(nullptr);   cout.tie(nullptr);
  20.   #ifndef ONLINE_JUDGE
  21.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  22.   #endif
  23.   }
  24.   vector <int> nums(1000);
  25.   bool binary_search (int l, int r, int num){
  26.       while(l <= r){
  27.         int mid = l + (r - l) / 2;
  28.         if(nums[mid] == num) return true;
  29.         (nums[mid] > num ? r = mid - 1 : l = mid + 1);
  30.       }
  31.       return false;
  32.   }
  33.   int main(){
  34.     Code_Crush();
  35.     int n, q;               cin >> n >> q;
  36.     nums.resize(n);
  37.     read(nums, n);
  38.     while(q--){
  39.         int item;           cin >> item;
  40.         cout << (binary_search(0, n, item) ? "YES\n" : "NO\n");
  41.     }
  42.     return 0;
  43.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement