rembocoder

Untitled

Feb 15th, 2023
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int binsearch(vector<int>& a, int x)
  6. {
  7. int n = a.size();
  8. int l = -1, r = n;
  9. while (r - l > 1)
  10. {
  11. int mid = (l + r) / 2;
  12. if (a[mid] >= x)
  13. {
  14. r = mid;
  15. }
  16. else
  17. {
  18. l = mid;
  19. }
  20. }
  21. return r;
  22. }
  23.  
  24. int main()
  25. {
  26. int n,m;
  27. cin >> n >> m;
  28. vector<int>a;
  29. for (int i = 0; i < n; i++)
  30. {
  31. int x;
  32. cin >> x;
  33. a.push_back(x);
  34. }
  35. for (int i = 0; i < m; i++)
  36. {
  37. int x;
  38. cin >> x;
  39.  
  40. int res = binsearch(a, x);
  41.  
  42. cout << (res < n && a[res] == x ? res : -1) << "\n";
  43. }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment