fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25.  
  26. vector<vector<int>> lamps;
  27. vector<int> points;
  28.  
  29. //* Assuming 0 based indexing lamps + points
  30. void consistency1(int n, int m) {
  31.  
  32. vector<int> dif(N);
  33. for(auto& l : lamps){
  34. int x = l[0];
  35. int y = l[1];
  36. dif[x]++;
  37. if(y+1<N) dif[y+1]--;
  38. }
  39.  
  40. for(int i=1; i<N; i++){
  41. dif[i] += dif[i-1];
  42. }
  43.  
  44. vector<int> ans(n);
  45. for(int i=0; i<n; i++){
  46. ans[i] = dif[points[i]];
  47. }
  48.  
  49. for(int i=0; i<n; i++){
  50. cout << ans[i] << " ";
  51. }
  52. cout << " | ";
  53.  
  54.  
  55. }
  56.  
  57. //* Assuming 0 based indexing lamps + points
  58. void consistency2(int n, int m) {
  59.  
  60. map<int,int> events;
  61. for(auto& l : lamps){
  62. int x = l[0], y = l[1];
  63.  
  64. events[x]++;
  65. events[y+1]--;
  66. }
  67.  
  68. map<int,int, greater<int>> mp;
  69. int acc = 0;
  70. for(auto& e : events){
  71. acc += e.second;
  72. mp[e.first] = acc;
  73. }
  74.  
  75. vector<int> ans(n, 0);
  76. for(int i=0; i<n; i++){
  77.  
  78. //* Some points might not exist in map so we find nearest point in map
  79. auto it = mp.lower_bound(points[i]);
  80. if(it!= mp.end()){
  81. ans[i] = it->second;
  82. }
  83. }
  84.  
  85. for(int i=0; i<n; i++){
  86. cout << ans[i] << " ";
  87. }
  88.  
  89. }
  90.  
  91. void solve() {
  92.  
  93. int n, m;
  94. cin >> n >> m;
  95.  
  96. points.resize(n);
  97. for(int i=0; i<n; i++) cin >> points[i];
  98.  
  99. lamps.resize(m);
  100. for(int i=0; i<m; i++){
  101. int x, y;
  102. cin >> x >> y;
  103. lamps[i] = {x, y};
  104. }
  105.  
  106. consistency1(n, m);
  107. consistency2(n, m);
  108. cout << endl;
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. int32_t main() {
  117. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  118.  
  119. int t = 1;
  120. cin >> t;
  121. while (t--) {
  122. solve();
  123. }
  124.  
  125. return 0;
  126. }
Success #stdin #stdout 0.01s 5484KB
stdin
1
6 3
7 1 5 10 9 15
1 7
5 11
7 9
stdout
3 1 2 1 2 0  | 3 1 2 1 2 0