fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[100005];
  4. int main()
  5. {
  6. int n, k;
  7. cin >> n >> k;
  8.  
  9. for(int i = 1; i <= n; i++)
  10. cin >> a[i];
  11.  
  12. while(k--){
  13. int x;
  14. cin >> x;
  15.  
  16. int l = 1, r = n, ans = 0;
  17.  
  18. while(l <= r){
  19. int mid = (l + r) / 2;
  20.  
  21. if(a[mid] > x){
  22. r = mid - 1;
  23. }
  24. else ans = mid, l = mid + 1;
  25. }
  26.  
  27. cout << ans << endl;
  28. }
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5288KB
stdin
5 5
3 3 5 8 9
2 4 8 1 10
stdout
0
2
4
0
5