fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAXN = 300005;
  5. int parent[MAXN + 1];
  6. int ans[MAXN];
  7.  
  8. int find(int i) {
  9. if (parent[i] == i) return i;
  10. return parent[i] = find(parent[i]);
  11. }
  12.  
  13. int main() {
  14. ios::sync_with_stdio(0); cin.tie(0);
  15. int n, m;
  16. cin >> n >> m;
  17.  
  18. for (int i = 1; i <= n + 1; i++) parent[i] = i;
  19.  
  20. for (int i = 0; i < m; i++) {
  21. int l, r, x;
  22. cin >> l >> r >> x;
  23.  
  24. int curr = find(l);
  25. while (curr <= r) {
  26. if (curr == x) {
  27. // Nếu là chiến binh thắng, ta chỉ cần nhảy sang người kế tiếp
  28. curr = find(curr + 1);
  29. } else {
  30. // Nếu là người thua, ta loại bỏ họ và nhảy tiếp
  31. ans[curr] = x;
  32. parent[curr] = find(curr + 1);
  33. curr = find(curr); // Tìm người tiếp theo sau khi đã cập nhật parent
  34. }
  35. }
  36. }
  37.  
  38. for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5592KB
stdin
8 4
3 5 4
3 7 6
2 8 8
1 8 1
stdout
0 8 4 6 4 8 6 1