Advertisement
LEGEND2004

I - Binary search - 1

Jul 27th, 2023
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. signed main()
  5. {
  6.     int n , q , x;
  7.     cin >> n >> q;
  8.     vector<int> a(n);
  9.     for(int i = 0; i < n; i++)
  10.         cin >> a[i];
  11.  
  12.     sort(a.begin() , a.end()); // O(n logn)
  13.  
  14.     while(q--){
  15.         cin >> x;
  16.         auto e = equal_range(a.begin() , a.end() , x);
  17.         cout << e.second - e.first << endl;
  18. /*
  19.         int l = lower_bound(a.begin() , a.end() , x) - a.begin();
  20.         int r = upper_bound(a.begin() , a.end() , x) - a.begin();
  21.         cout << (r - l) << endl;*/
  22.     }
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement