fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int64_t inf = (int64_t)1e18 + 42;
  5.  
  6. template<class T, bool (*cmp)(T, T)>
  7. class Heap {
  8. private:
  9. vector<T> heap_values;
  10. vector<int> heap, pos_in_heap;
  11.  
  12. void push_up(int id) {
  13. while(id > 0 &&
  14. cmp(heap_values[heap[id]], heap_values[heap[(id - 1) / 2]])) {
  15. swap(heap[id], heap[(id - 1) / 2]);
  16. swap(pos_in_heap[heap[id]], pos_in_heap[heap[(id - 1) / 2]]);
  17. id = (id - 1) / 2;
  18. }
  19. }
  20.  
  21. void push_down(int id) {
  22. while(2 * id + 1 < heap.size()) {
  23. int child = 2 * id + 1;
  24. if(child + 1 < heap.size() &&
  25. cmp(heap_values[heap[child + 1]], heap_values[heap[child]])) {
  26. child++;
  27. }
  28. if(cmp(heap_values[heap[id]], heap_values[heap[child]])) {
  29. break;
  30. }
  31. swap(heap[id], heap[child]);
  32. swap(pos_in_heap[heap[id]], pos_in_heap[heap[child]]);
  33. id = child;
  34. }
  35. }
  36.  
  37. public:
  38. Heap() { clear(); }
  39.  
  40. void clear() {
  41. heap.clear();
  42. heap_values.clear();
  43. pos_in_heap.clear();
  44. }
  45.  
  46. int push(T val) {
  47. heap.push_back(heap_values.size());
  48. pos_in_heap.push_back(heap.size() - 1);
  49. heap_values.push_back(val);
  50. push_up(heap.size() - 1);
  51. return heap_values.size() - 1;
  52. }
  53.  
  54. T pop() {
  55. int ret_node = heap[0];
  56. swap(pos_in_heap[ret_node], pos_in_heap[heap.back()]);
  57. swap(heap[0], heap[heap.size() - 1]);
  58. heap.pop_back();
  59. pos_in_heap[ret_node] = -1;
  60. if(heap.size() > 0) {
  61. push_down(0);
  62. }
  63. return heap_values[ret_node];
  64. }
  65.  
  66. size_t size() { return heap.size(); }
  67. bool empty() { return heap.size() == 0; }
  68.  
  69. T top() { return heap_values[heap[0]]; }
  70.  
  71. int top_node() { return heap[0]; }
  72.  
  73. void update(int node, T val) {
  74. int p = pos_in_heap[node];
  75. bool is_push_down = cmp(heap_values[node], val);
  76.  
  77. if(is_push_down) {
  78. heap_values[node] = val;
  79. push_down(p);
  80. } else {
  81. heap_values[node] = val;
  82. push_up(p);
  83. }
  84. }
  85. };
  86.  
  87. int n, m, real_n;
  88. vector<tuple<int, int, int>> edges;
  89. vector<vector<pair<int, int>>> adj;
  90.  
  91. void read() {
  92. cin >> n >> m;
  93. adj.assign(5 * n + 1, {});
  94. real_n = n;
  95.  
  96. for(int i = 0; i < m; i++) {
  97. int u, l, r;
  98. cin >> u >> l >> r;
  99. u--, l--, r--;
  100. edges.emplace_back(u, l, r);
  101. }
  102. }
  103.  
  104. vector<int> node;
  105.  
  106. int build_tree_struct(int l, int r, int idx) {
  107. if(l == r) {
  108. int v = n++;
  109. adj[v].push_back({l, 1});
  110. return node[idx] = v;
  111. }
  112.  
  113. int mid = (l + r) / 2;
  114. int v = n++;
  115. adj[v].push_back({build_tree_struct(l, mid, 2 * idx + 1), 0});
  116. adj[v].push_back({build_tree_struct(mid + 1, r, 2 * idx + 2), mid - l + 1});
  117. return node[idx] = v;
  118. }
  119.  
  120. void add_edges(int u, int ql, int qr, int l, int r, int idx) {
  121. if(ql > r || qr < l) {
  122. return;
  123. }
  124. if(ql <= l && r <= qr) {
  125. adj[u].push_back({node[idx], l - ql});
  126. return;
  127. }
  128.  
  129. int mid = (l + r) / 2;
  130. add_edges(u, ql, qr, l, mid, 2 * idx + 1);
  131. add_edges(u, ql, qr, mid + 1, r, 2 * idx + 2);
  132. }
  133.  
  134. bool cmp_min(int64_t a, int64_t b) { return a < b; }
  135.  
  136. vector<int64_t> dijkstra(int start) {
  137. vector<int64_t> dist(n, inf);
  138. vector<int> pq_node(n, -1);
  139. vector<int> pq_node_to_ver(n, -1);
  140. Heap<int64_t, cmp_min> pq;
  141.  
  142. function<void(int, int64_t)> update = [&](int u, int64_t d) {
  143. dist[u] = min(dist[u], d);
  144. if(pq_node[u] == -1) {
  145. pq_node[u] = pq.push(dist[u]);
  146. pq_node_to_ver[pq_node[u]] = u;
  147. } else {
  148. pq.update(pq_node[u], dist[u]);
  149. }
  150. };
  151.  
  152. update(start, 0);
  153. while(!pq.empty()) {
  154. int u = pq_node_to_ver[pq.top_node()];
  155. pq.pop();
  156.  
  157. for(auto [v, w]: adj[u]) {
  158. update(v, dist[u] + w);
  159. }
  160. }
  161.  
  162. return dist;
  163. }
  164.  
  165. void solve() {
  166. node.resize(4 * real_n + 1);
  167. build_tree_struct(0, real_n - 1, 0);
  168.  
  169. for(auto [u, l, r]: edges) {
  170. add_edges(u, l, r, 0, real_n - 1, 0);
  171. }
  172.  
  173. vector<int64_t> ans = dijkstra(0);
  174. for(int i = 0; i < real_n; i++) {
  175. int64_t res = ans[i];
  176. if(res == inf) {
  177. cout << -1 << ' ';
  178. } else {
  179. cout << res << ' ';
  180. }
  181. }
  182. cout << '\n';
  183. }
  184.  
  185. int main() {
  186. ios_base::sync_with_stdio(false);
  187. cin.tie(nullptr);
  188.  
  189. read();
  190. solve();
  191. return 0;
  192. }
  193.  
Success #stdin #stdout 0.01s 5328KB
stdin
8 4
1 2 3
1 5 8
3 8 8
2 5 8
stdout
0 1 2 -1 1 2 3 3