Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<map>
  4. using namespace std;
  5.  
  6. class Streak
  7. {
  8. public:
  9. int streak_start, streak_end;
  10.  
  11. Streak()
  12. {
  13. streak_start = streak_end = -1;
  14. }
  15. Streak(int streak_start, int streak_end)
  16. {
  17. this->streak_start = streak_start;
  18. this->streak_end = streak_end;
  19. }
  20. };
  21.  
  22. Streak get_streak_details(map<int, int>& streaks, int idx)
  23. {
  24. Streak s;
  25. if(streaks.empty())return s;
  26.  
  27. auto lb = streaks.lower_bound(idx);
  28.  
  29. if(lb == streaks.begin() and lb->first != idx)return s;
  30.  
  31. if(lb == streaks.end() or lb->first > idx)
  32. {
  33. lb--;
  34. }
  35.  
  36. if(lb->first <= idx and idx <= lb->second)
  37. {
  38. s.streak_start = lb->first;
  39. s.streak_end = lb->second;
  40. }
  41.  
  42. return s;
  43. }
  44.  
  45. int main()
  46. {
  47. int n, q;
  48. cin >> n >> q;
  49.  
  50. vector<int> left(q);
  51. vector<int> right(q);
  52. vector<int> color_to_fill(q);
  53.  
  54. int idx = q - 1;
  55.  
  56. while(q--)
  57. {
  58. int l, r, c;
  59. cin >> l >> r >> c;
  60.  
  61. left[idx] = l;
  62. right[idx] = r;
  63. color_to_fill[idx] = c;
  64.  
  65. idx--;
  66. }
  67.  
  68. vector<int> color(n, 0);
  69.  
  70. // A map, this contains start and end of each streak. initially it's empty and going forward we will maintain streaks
  71. // this also provides us
  72. // O(logN) searches for next idx to fill, start and end of a streak
  73. // O(logN) deletion and insertion for merging streaks
  74. map<int, int> streaks;
  75.  
  76. for(int i = 0; i < left.size(); i++)
  77. {
  78. int curr_idx = left[i];
  79. int r = right[i];
  80. int c = color_to_fill[i];
  81.  
  82. while(curr_idx <= r)
  83. {
  84. if(color[curr_idx - 1] != 0)
  85. {
  86. // that means this curr_idx is part of some streak
  87. // auto lb = streaks.lower_bound(curr_idx);
  88.  
  89. // if(lb->first > curr_idx)
  90. // {
  91. // // then curr_idx is part of immediate prev streak
  92. // lb--;
  93. // }
  94.  
  95. auto streak = get_streak_details(streaks, curr_idx);
  96.  
  97. // curr_idx = lb->second + 1;
  98. curr_idx = streak.streak_end + 1;
  99. continue;
  100. }
  101.  
  102. // now the curr_idx is not part of any streak
  103. // so we fill the color as c
  104. // and merge streaks as needed
  105.  
  106. color[curr_idx - 1] = c;
  107.  
  108. // merge streaks
  109. auto prev_streak = get_streak_details(streaks, curr_idx - 1);
  110. auto next_streak = get_streak_details(streaks, curr_idx + 1);
  111.  
  112. bool prev_streak_exists = (prev_streak.streak_start != -1) ? true : false;
  113. bool next_streak_exists = (next_streak.streak_start != -1) ? true : false;
  114.  
  115. if(prev_streak_exists and !next_streak_exists)
  116. {
  117. streaks.erase(prev_streak.streak_start);
  118. streaks.insert({prev_streak.streak_start, curr_idx});
  119. curr_idx++;
  120. }
  121. else if(!prev_streak_exists and next_streak_exists)
  122. {
  123. streaks.erase(next_streak.streak_start);
  124. streaks.insert({curr_idx, next_streak.streak_end});
  125. curr_idx = next_streak.streak_end + 1;
  126. }
  127. else if(prev_streak_exists and next_streak_exists)
  128. {
  129. streaks.erase(next_streak.streak_start);
  130. streaks.erase(prev_streak.streak_start);
  131. streaks.insert({prev_streak.streak_start, next_streak.streak_end});
  132. curr_idx = next_streak.streak_end + 1;
  133. }
  134. else
  135. {
  136. streaks.insert({curr_idx, curr_idx});
  137. curr_idx++;
  138. }
  139. }
  140. }
  141.  
  142. for(int i = 0; i < n; i++) cout << color[i] << endl;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement