Advertisement
LEGEND2004

Binary search

Jul 20th, 2023
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. signed main()
  7. {
  8.     int n , q , x;
  9.     cin >> n >> q;
  10.     vector<int> a(n);
  11.     for(int i = 0; i < n; i++){
  12.         cin >> a[i];
  13.     }
  14.     while(q--){
  15.         cin >> x;
  16.         int l = 0 , r = n - 1;
  17.         while(l < r){
  18.             int mid = (l + r) / 2;
  19.             if(a[mid] < x){
  20.                 l = mid + 1;
  21.             }else{
  22.                 r = mid;
  23.             }
  24.         }
  25.         //l == r
  26.         cout << ((a[l] == x) ? "YES" : "NO") << endl;
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement