#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif void brute(int n, vector<tuple<int, int, int>> edges, vector<tuple<int, int, LL, int>> queries) { vector<set<pair<int, int>>> graph(n); for (auto [u, v, d] : edges) { --u; --v; graph[u].emplace(v, d); graph[v].emplace(u, d); } vector<int> color(n); for (auto [type, a, b, c] : queries) { if (type == 1) { --a; --b; graph[a].emplace(b, c); graph[b].emplace(a, c); } else if (type == 2) { --a; --b; graph[a].erase(graph[a].lower_bound(pair(b, 0))); graph[b].erase(graph[b].lower_bound(pair(a, 0))); } else if (type == 3) { --a; function<void(int, int, LL)> dfs = [&](int v, int par, LL dist) { color[v] = c; for (auto [u, d] : graph[v]) { if (u == par || dist + d > b) continue; dfs(u, v, dist + d); } }; dfs(a, -1, 0); } else { --a; cout << color[a] << '\n'; } } } struct CentroDecomp { const vector<vector<pair<int, int>>> &graph; // tu vector<int> par, podsz, odwi; vector<int> dep; vector<vector<LL>> dist; vector<priority_queue<pair<LL, int>>> col; int odwi_cnt = 1; const int INF = int(1e9); int root; void refresh() { ++odwi_cnt; } void visit(int v) { odwi[v] = max(odwi[v], odwi_cnt); } bool is_vis(int v) { return odwi[v] >= odwi_cnt; } void dfs_podsz(int v) { visit(v); podsz[v] = 1; for (auto [u, d] : graph[v]) // tu if (!is_vis(u)) { dfs_podsz(u); podsz[v] += podsz[u]; } } int centro(int v) { refresh(); dfs_podsz(v); int sz = podsz[v] / 2; refresh(); while (true) { visit(v); for (auto [u, d] : graph[v]) // tu if (!is_vis(u) && podsz[u] > sz) { v = u; break; } if (is_vis(v)) return v; } } void dfs_dist(int v, LL dst, int gl) { visit(v); dist[gl][v] = dst; for (auto [u, d] : graph[v]) if (!is_vis(u)) dfs_dist(u, dst + d, gl); } void decomp(int v) { refresh(); // Tu kod. Centroid to v, ktory jest juz dozywotnie odwiedzony. refresh(); dist[dep[v]][v] = 0; for (auto [u, d] : graph[v]) { if (!is_vis(u)) { dfs_dist(u, d, dep[v]); } } // Koniec kodu. refresh(); for(auto [u, d] : graph[v]) // tu if (!is_vis(u)) { u = centro(u); par[u] = v; odwi[u] = INF; dep[u] = dep[v] + 1; // Opcjonalnie tutaj przekazujemy info synowi w drzewie CD. decomp(u); } } CentroDecomp(int n, vector<vector<pair<int, int>>> &grph) // tu : graph(grph), par(n, -1), podsz(n), odwi(n) { dist = vector(30, vector(n, 0ll)); col = vector<priority_queue<pair<LL, int>>>(n); dep = vector(n, 0); root = centro(0); odwi[root] = INF; decomp(root); debug(dist); debug(dep); } vector<int> collect(int v, LL w) { vector<int> ret; int u = v; while (u != -1) { LL cur_w = w - dist[dep[u]][v]; debug(v, cur_w, dep[u], dist[dep[u]][v]); while (ssize(col[u])) { auto [odl, id] = col[u].top(); if (-odl <= cur_w) { col[u].pop(); ret.emplace_back(id); } else break; } u = par[u]; debug(ret); } return ret; } void toss(int v, int id) { int w = v; while (w != -1) { col[w].emplace(-dist[dep[w]][v], id); w = par[w]; } } }; void sub1(int n, vector<tuple<int, int, int>> edges, vector<tuple<int, int, LL, int>> queries) { vector<vector<pair<int, int>>> graph(n); for (auto [u, v, d] : edges) { --u; --v; graph[u].emplace_back(v, d); graph[v].emplace_back(u, d); } CentroDecomp cd(n, graph); int q = ssize(queries); vector<int> is_ans(q); vector<pair<int, int>> ans; for (int i = ssize(queries) - 1; i >= 0; --i) { auto [type, a, b, c] = queries[i]; assert(type != 1 && type != 2); if (type == 3) { --a; auto vec = cd.collect(a, b); for (int w : vec) { if (is_ans[w]) continue; is_ans[w] = 1; ans.emplace_back(w, c); } } else { --a; cd.toss(a, i); } } REP (i, q) { auto [type, a, b, c] = queries[i]; if (type == 4 && !is_ans[i]) ans.emplace_back(i, 0); } sort(ans.begin(), ans.end()); for (auto [a, b] : ans) cout << b << '\n'; } /* * Opis: O(q \log n) Link-Cut Tree z wyznaczaniem odległości między wierzchołkami, lca w zakorzenionym drzewie, dodawaniem na ścieżce, dodawaniem na poddrzewie, zwracaniem sumy na ścieżce, zwracaniem sumy na poddrzewie. * Przepisać co się chce (logika lazy jest tylko w \texttt{AdditionalInfo}, można np. zostawić puste funkcje). * Wywołać konstruktor, potem \texttt{set\_value} na wierzchołkach (aby się ustawiło, że nie-nil to nie-nil) i potem jazda. */ using T = pair<int, int>; T merge(T a, T b) { if (a.second > b.second) return a; return b; } struct AdditionalInfo { static constexpr T neutral = pair(0, -1); // Remember that there is a nil vertex! T node_value = neutral, splay_value = neutral;//, splay_value_reversed = neutral; T splay_lazy = neutral; // lazy propagation on paths int splay_size = 0; // 0 because of nil T whole_subtree_lazy = neutral, whole_subtree_cancel = neutral; // lazy propagation on subtrees void set_value(T x) { node_value = splay_value = x; splay_size = 1; } void update_from_sons(AdditionalInfo &l, AdditionalInfo &r) { splay_value = merge(l.splay_value, merge(node_value, r.splay_value)); splay_size = l.splay_size + 1 + r.splay_size; } void push_lazy(AdditionalInfo &l, AdditionalInfo &r, bool /*reversed children*/) { l.add_lazy_in_path(splay_lazy); r.add_lazy_in_path(splay_lazy); splay_lazy = neutral; } void cancel_subtree_lazy_from_parent(AdditionalInfo &parent) { whole_subtree_cancel = parent.whole_subtree_lazy; } void pull_lazy_from_parent(AdditionalInfo &parent) { if(splay_size == 0) // nil return; if (whole_subtree_cancel.second < parent.whole_subtree_lazy.second) add_lazy_in_subtree(parent.whole_subtree_lazy); cancel_subtree_lazy_from_parent(parent); } void add_lazy_in_path(T x) { splay_lazy = merge(splay_lazy, x); node_value = merge(node_value, x); splay_value = merge(splay_value, x); } void add_lazy_in_subtree(T x) { whole_subtree_lazy = merge(whole_subtree_lazy, x); node_value = merge(node_value, x); splay_value = merge(splay_value, x); } }; struct Splay { struct Node { array<int, 2> child; int parent; int subsize_splay = 1; bool lazy_flip = false; AdditionalInfo info; }; vector<Node> t; const int nil; Splay(int n) : t(n + 1), nil(n) { t[nil].subsize_splay = 0; for(Node &v : t) v.child[0] = v.child[1] = v.parent = nil; } void apply_lazy_and_push(int v) { auto &[l, r] = t[v].child; if(t[v].lazy_flip) { for(int c : {l, r}) t[c].lazy_flip ^= 1; swap(l, r); } t[v].info.push_lazy(t[l].info, t[r].info, t[v].lazy_flip); for(int c : {l, r}) if(c != nil) t[c].info.pull_lazy_from_parent(t[v].info); t[v].lazy_flip = false; } void update_from_sons(int v) { // assumes that v's info is pushed auto [l, r] = t[v].child; t[v].subsize_splay = t[l].subsize_splay + 1 + t[r].subsize_splay; for(int c : {l, r}) apply_lazy_and_push(c); t[v].info.update_from_sons(t[l].info, t[r].info); } // After that, v is pushed and updated void splay(int v) { apply_lazy_and_push(v); auto set_child = [&](int x, int c, int d) { if(x != nil and d != -1) t[x].child[d] = c; if(c != nil) { t[c].parent = x; t[c].info.cancel_subtree_lazy_from_parent(t[x].info); } }; auto get_dir = [&](int x) -> int { int p = t[x].parent; if(p == nil or (x != t[p].child[0] and x != t[p].child[1])) return -1; return t[p].child[1] == x; }; auto rotate = [&](int x, int d) { int p = t[x].parent, c = t[x].child[d]; assert(c != nil); set_child(p, c, get_dir(x)); set_child(x, t[c].child[!d], d); set_child(c, x, !d); update_from_sons(x); update_from_sons(c); }; while(get_dir(v) != -1) { int p = t[v].parent, pp = t[p].parent; array path_up = {v, p, pp, t[pp].parent}; for(int i = ssize(path_up) - 1; i >= 0; --i) { if(i < ssize(path_up) - 1) t[path_up[i]].info.pull_lazy_from_parent(t[path_up[i + 1]].info); apply_lazy_and_push(path_up[i]); } int dp = get_dir(v), dpp = get_dir(p); if(dpp == -1) rotate(p, dp); else if(dp == dpp) { rotate(pp, dpp); rotate(p, dp); } else { rotate(p, dp); rotate(pp, dpp); } } } }; struct LinkCut : Splay { LinkCut(int n) : Splay(n) {} // Cuts the path from x downward, creates path to root, splays x. int access(int x) { int v = x, cv = nil; for(; v != nil; cv = v, v = t[v].parent) { splay(v); int &right = t[v].child[1]; right = cv; t[right].info.pull_lazy_from_parent(t[v].info); update_from_sons(v); } splay(x); return cv; } // Changes the root to v. // Warning: Linking, cutting, getting the distance, etc, changes the root. void reroot(int v) { access(v); t[v].lazy_flip ^= 1; apply_lazy_and_push(v); } // Returns the root of tree containing v. int get_leader(int v) { access(v); while(apply_lazy_and_push(v), t[v].child[0] != nil) v = t[v].child[0]; splay(v); return v; } bool is_in_same_tree(int v, int u) { return get_leader(v) == get_leader(u); } // Assumes that v and u aren't in same tree and v != u. // Adds edge (v, u) to the forest. void link(int v, int u) { reroot(v); access(u); assert(t[v].parent == nil); t[v].parent = u; t[v].info.cancel_subtree_lazy_from_parent(t[u].info); } // Assumes that v and u are in same tree and v != u. // Cuts edge going from v to the subtree where is u // (in particular, if there is an edge (v, u), it deletes it). // Returns the cut parent. int cut(int v, int u) { reroot(u); access(v); int c = t[v].child[0]; assert(t[c].parent == v); t[v].child[0] = nil; t[c].parent = nil; t[c].info.cancel_subtree_lazy_from_parent(t[nil].info); update_from_sons(v); while(apply_lazy_and_push(c), t[c].child[1] != nil) c = t[c].child[1]; splay(c); return c; } // Assumes that v and u are in same tree. // Returns their LCA after a reroot operation. int lca(int root, int v, int u) { reroot(root); if(v == u) return v; access(v); return access(u); } // Assumes that v and u are in same tree. // Returns their distance (in number of edges). int dist(int v, int u) { reroot(v); access(u); return t[t[u].child[0]].subsize_splay; } // Applies function f on vertex v (useful for a single add/set operation) void apply_on_vertex(int v, function<void (AdditionalInfo&)> f) { access(v); f(t[v].info); } // Assumes that v and u are in same tree. // Adds val to each vertex in path from v to u. void add_on_path(int v, int u, T val) { reroot(v); access(u); t[u].info.add_lazy_in_path(val); } // Assumes that v and u are in same tree. // Adds val to each vertex in subtree of v that doesn't have u. void add_on_subtree(int v, int u, T val) { u = cut(v, u); t[v].info.add_lazy_in_subtree(val); link(v, u); } }; void sub2(int n, vector<tuple<int, int, int>> edges, vector<tuple<int, int, LL, int>> queries) { LinkCut cat(n); for (auto [a, b, d] : edges) { --a; --b; cat.link(a, b); } int tim = 0; for (auto [type, a, b, c] : queries) { ++tim; if (type == 1) { int u = a - 1; int v = b - 1; cat.link(u, v); } else if (type == 2) { int u = a - 1; int v = b - 1; cat.cut(u, v); } else if (type == 3) { int v = a - 1; cat.reroot(v); cat.t[v].info.add_lazy_in_subtree(pair(c, tim)); } else { int v = a - 1; cat.reroot(v); cout << cat.t[v].info.node_value.first << '\n'; } } } int main() { cin.tie(0)->sync_with_stdio(0); int n, m, q; cin >> n >> m >> q; vector<tuple<int, int, int>> edges(m); for (auto &[a, b, c] : edges) cin >> a >> b >> c; vector<tuple<int, int, LL, int>> queries; REP (xd, q) { int type; cin >> type; if (type == 1) { int a, b, d; cin >> a >> b >> d; queries.emplace_back(type, a, b, d); } else if (type == 2) { int a, b; cin >> a >> b; queries.emplace_back(type, a, b, 0); } else if (type == 3) { int a; LL b; int c; cin >> a >> b >> c; queries.emplace_back(type, a, b, c); } else { int a; cin >> a; queries.emplace_back(type, a, 0, 0); } } bool is_first = m == (n - 1); for (auto [type, a, b, c] : queries) if (type == 1 || type == 2) is_first = false; if (is_first) { sub1(n, edges, queries); return 0; } bool is_second = true; const LL Z = 1'000'000'000'000'000ll; for (auto [type, a, b, c] : queries) if (type == 3 && b != Z) is_second = false; if (is_second) { sub2(n, edges, queries); return 0; } brute(n, edges, queries); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif void brute(int n, vector<tuple<int, int, int>> edges, vector<tuple<int, int, LL, int>> queries) { vector<set<pair<int, int>>> graph(n); for (auto [u, v, d] : edges) { --u; --v; graph[u].emplace(v, d); graph[v].emplace(u, d); } vector<int> color(n); for (auto [type, a, b, c] : queries) { if (type == 1) { --a; --b; graph[a].emplace(b, c); graph[b].emplace(a, c); } else if (type == 2) { --a; --b; graph[a].erase(graph[a].lower_bound(pair(b, 0))); graph[b].erase(graph[b].lower_bound(pair(a, 0))); } else if (type == 3) { --a; function<void(int, int, LL)> dfs = [&](int v, int par, LL dist) { color[v] = c; for (auto [u, d] : graph[v]) { if (u == par || dist + d > b) continue; dfs(u, v, dist + d); } }; dfs(a, -1, 0); } else { --a; cout << color[a] << '\n'; } } } struct CentroDecomp { const vector<vector<pair<int, int>>> &graph; // tu vector<int> par, podsz, odwi; vector<int> dep; vector<vector<LL>> dist; vector<priority_queue<pair<LL, int>>> col; int odwi_cnt = 1; const int INF = int(1e9); int root; void refresh() { ++odwi_cnt; } void visit(int v) { odwi[v] = max(odwi[v], odwi_cnt); } bool is_vis(int v) { return odwi[v] >= odwi_cnt; } void dfs_podsz(int v) { visit(v); podsz[v] = 1; for (auto [u, d] : graph[v]) // tu if (!is_vis(u)) { dfs_podsz(u); podsz[v] += podsz[u]; } } int centro(int v) { refresh(); dfs_podsz(v); int sz = podsz[v] / 2; refresh(); while (true) { visit(v); for (auto [u, d] : graph[v]) // tu if (!is_vis(u) && podsz[u] > sz) { v = u; break; } if (is_vis(v)) return v; } } void dfs_dist(int v, LL dst, int gl) { visit(v); dist[gl][v] = dst; for (auto [u, d] : graph[v]) if (!is_vis(u)) dfs_dist(u, dst + d, gl); } void decomp(int v) { refresh(); // Tu kod. Centroid to v, ktory jest juz dozywotnie odwiedzony. refresh(); dist[dep[v]][v] = 0; for (auto [u, d] : graph[v]) { if (!is_vis(u)) { dfs_dist(u, d, dep[v]); } } // Koniec kodu. refresh(); for(auto [u, d] : graph[v]) // tu if (!is_vis(u)) { u = centro(u); par[u] = v; odwi[u] = INF; dep[u] = dep[v] + 1; // Opcjonalnie tutaj przekazujemy info synowi w drzewie CD. decomp(u); } } CentroDecomp(int n, vector<vector<pair<int, int>>> &grph) // tu : graph(grph), par(n, -1), podsz(n), odwi(n) { dist = vector(30, vector(n, 0ll)); col = vector<priority_queue<pair<LL, int>>>(n); dep = vector(n, 0); root = centro(0); odwi[root] = INF; decomp(root); debug(dist); debug(dep); } vector<int> collect(int v, LL w) { vector<int> ret; int u = v; while (u != -1) { LL cur_w = w - dist[dep[u]][v]; debug(v, cur_w, dep[u], dist[dep[u]][v]); while (ssize(col[u])) { auto [odl, id] = col[u].top(); if (-odl <= cur_w) { col[u].pop(); ret.emplace_back(id); } else break; } u = par[u]; debug(ret); } return ret; } void toss(int v, int id) { int w = v; while (w != -1) { col[w].emplace(-dist[dep[w]][v], id); w = par[w]; } } }; void sub1(int n, vector<tuple<int, int, int>> edges, vector<tuple<int, int, LL, int>> queries) { vector<vector<pair<int, int>>> graph(n); for (auto [u, v, d] : edges) { --u; --v; graph[u].emplace_back(v, d); graph[v].emplace_back(u, d); } CentroDecomp cd(n, graph); int q = ssize(queries); vector<int> is_ans(q); vector<pair<int, int>> ans; for (int i = ssize(queries) - 1; i >= 0; --i) { auto [type, a, b, c] = queries[i]; assert(type != 1 && type != 2); if (type == 3) { --a; auto vec = cd.collect(a, b); for (int w : vec) { if (is_ans[w]) continue; is_ans[w] = 1; ans.emplace_back(w, c); } } else { --a; cd.toss(a, i); } } REP (i, q) { auto [type, a, b, c] = queries[i]; if (type == 4 && !is_ans[i]) ans.emplace_back(i, 0); } sort(ans.begin(), ans.end()); for (auto [a, b] : ans) cout << b << '\n'; } /* * Opis: O(q \log n) Link-Cut Tree z wyznaczaniem odległości między wierzchołkami, lca w zakorzenionym drzewie, dodawaniem na ścieżce, dodawaniem na poddrzewie, zwracaniem sumy na ścieżce, zwracaniem sumy na poddrzewie. * Przepisać co się chce (logika lazy jest tylko w \texttt{AdditionalInfo}, można np. zostawić puste funkcje). * Wywołać konstruktor, potem \texttt{set\_value} na wierzchołkach (aby się ustawiło, że nie-nil to nie-nil) i potem jazda. */ using T = pair<int, int>; T merge(T a, T b) { if (a.second > b.second) return a; return b; } struct AdditionalInfo { static constexpr T neutral = pair(0, -1); // Remember that there is a nil vertex! T node_value = neutral, splay_value = neutral;//, splay_value_reversed = neutral; T splay_lazy = neutral; // lazy propagation on paths int splay_size = 0; // 0 because of nil T whole_subtree_lazy = neutral, whole_subtree_cancel = neutral; // lazy propagation on subtrees void set_value(T x) { node_value = splay_value = x; splay_size = 1; } void update_from_sons(AdditionalInfo &l, AdditionalInfo &r) { splay_value = merge(l.splay_value, merge(node_value, r.splay_value)); splay_size = l.splay_size + 1 + r.splay_size; } void push_lazy(AdditionalInfo &l, AdditionalInfo &r, bool /*reversed children*/) { l.add_lazy_in_path(splay_lazy); r.add_lazy_in_path(splay_lazy); splay_lazy = neutral; } void cancel_subtree_lazy_from_parent(AdditionalInfo &parent) { whole_subtree_cancel = parent.whole_subtree_lazy; } void pull_lazy_from_parent(AdditionalInfo &parent) { if(splay_size == 0) // nil return; if (whole_subtree_cancel.second < parent.whole_subtree_lazy.second) add_lazy_in_subtree(parent.whole_subtree_lazy); cancel_subtree_lazy_from_parent(parent); } void add_lazy_in_path(T x) { splay_lazy = merge(splay_lazy, x); node_value = merge(node_value, x); splay_value = merge(splay_value, x); } void add_lazy_in_subtree(T x) { whole_subtree_lazy = merge(whole_subtree_lazy, x); node_value = merge(node_value, x); splay_value = merge(splay_value, x); } }; struct Splay { struct Node { array<int, 2> child; int parent; int subsize_splay = 1; bool lazy_flip = false; AdditionalInfo info; }; vector<Node> t; const int nil; Splay(int n) : t(n + 1), nil(n) { t[nil].subsize_splay = 0; for(Node &v : t) v.child[0] = v.child[1] = v.parent = nil; } void apply_lazy_and_push(int v) { auto &[l, r] = t[v].child; if(t[v].lazy_flip) { for(int c : {l, r}) t[c].lazy_flip ^= 1; swap(l, r); } t[v].info.push_lazy(t[l].info, t[r].info, t[v].lazy_flip); for(int c : {l, r}) if(c != nil) t[c].info.pull_lazy_from_parent(t[v].info); t[v].lazy_flip = false; } void update_from_sons(int v) { // assumes that v's info is pushed auto [l, r] = t[v].child; t[v].subsize_splay = t[l].subsize_splay + 1 + t[r].subsize_splay; for(int c : {l, r}) apply_lazy_and_push(c); t[v].info.update_from_sons(t[l].info, t[r].info); } // After that, v is pushed and updated void splay(int v) { apply_lazy_and_push(v); auto set_child = [&](int x, int c, int d) { if(x != nil and d != -1) t[x].child[d] = c; if(c != nil) { t[c].parent = x; t[c].info.cancel_subtree_lazy_from_parent(t[x].info); } }; auto get_dir = [&](int x) -> int { int p = t[x].parent; if(p == nil or (x != t[p].child[0] and x != t[p].child[1])) return -1; return t[p].child[1] == x; }; auto rotate = [&](int x, int d) { int p = t[x].parent, c = t[x].child[d]; assert(c != nil); set_child(p, c, get_dir(x)); set_child(x, t[c].child[!d], d); set_child(c, x, !d); update_from_sons(x); update_from_sons(c); }; while(get_dir(v) != -1) { int p = t[v].parent, pp = t[p].parent; array path_up = {v, p, pp, t[pp].parent}; for(int i = ssize(path_up) - 1; i >= 0; --i) { if(i < ssize(path_up) - 1) t[path_up[i]].info.pull_lazy_from_parent(t[path_up[i + 1]].info); apply_lazy_and_push(path_up[i]); } int dp = get_dir(v), dpp = get_dir(p); if(dpp == -1) rotate(p, dp); else if(dp == dpp) { rotate(pp, dpp); rotate(p, dp); } else { rotate(p, dp); rotate(pp, dpp); } } } }; struct LinkCut : Splay { LinkCut(int n) : Splay(n) {} // Cuts the path from x downward, creates path to root, splays x. int access(int x) { int v = x, cv = nil; for(; v != nil; cv = v, v = t[v].parent) { splay(v); int &right = t[v].child[1]; right = cv; t[right].info.pull_lazy_from_parent(t[v].info); update_from_sons(v); } splay(x); return cv; } // Changes the root to v. // Warning: Linking, cutting, getting the distance, etc, changes the root. void reroot(int v) { access(v); t[v].lazy_flip ^= 1; apply_lazy_and_push(v); } // Returns the root of tree containing v. int get_leader(int v) { access(v); while(apply_lazy_and_push(v), t[v].child[0] != nil) v = t[v].child[0]; splay(v); return v; } bool is_in_same_tree(int v, int u) { return get_leader(v) == get_leader(u); } // Assumes that v and u aren't in same tree and v != u. // Adds edge (v, u) to the forest. void link(int v, int u) { reroot(v); access(u); assert(t[v].parent == nil); t[v].parent = u; t[v].info.cancel_subtree_lazy_from_parent(t[u].info); } // Assumes that v and u are in same tree and v != u. // Cuts edge going from v to the subtree where is u // (in particular, if there is an edge (v, u), it deletes it). // Returns the cut parent. int cut(int v, int u) { reroot(u); access(v); int c = t[v].child[0]; assert(t[c].parent == v); t[v].child[0] = nil; t[c].parent = nil; t[c].info.cancel_subtree_lazy_from_parent(t[nil].info); update_from_sons(v); while(apply_lazy_and_push(c), t[c].child[1] != nil) c = t[c].child[1]; splay(c); return c; } // Assumes that v and u are in same tree. // Returns their LCA after a reroot operation. int lca(int root, int v, int u) { reroot(root); if(v == u) return v; access(v); return access(u); } // Assumes that v and u are in same tree. // Returns their distance (in number of edges). int dist(int v, int u) { reroot(v); access(u); return t[t[u].child[0]].subsize_splay; } // Applies function f on vertex v (useful for a single add/set operation) void apply_on_vertex(int v, function<void (AdditionalInfo&)> f) { access(v); f(t[v].info); } // Assumes that v and u are in same tree. // Adds val to each vertex in path from v to u. void add_on_path(int v, int u, T val) { reroot(v); access(u); t[u].info.add_lazy_in_path(val); } // Assumes that v and u are in same tree. // Adds val to each vertex in subtree of v that doesn't have u. void add_on_subtree(int v, int u, T val) { u = cut(v, u); t[v].info.add_lazy_in_subtree(val); link(v, u); } }; void sub2(int n, vector<tuple<int, int, int>> edges, vector<tuple<int, int, LL, int>> queries) { LinkCut cat(n); for (auto [a, b, d] : edges) { --a; --b; cat.link(a, b); } int tim = 0; for (auto [type, a, b, c] : queries) { ++tim; if (type == 1) { int u = a - 1; int v = b - 1; cat.link(u, v); } else if (type == 2) { int u = a - 1; int v = b - 1; cat.cut(u, v); } else if (type == 3) { int v = a - 1; cat.reroot(v); cat.t[v].info.add_lazy_in_subtree(pair(c, tim)); } else { int v = a - 1; cat.reroot(v); cout << cat.t[v].info.node_value.first << '\n'; } } } int main() { cin.tie(0)->sync_with_stdio(0); int n, m, q; cin >> n >> m >> q; vector<tuple<int, int, int>> edges(m); for (auto &[a, b, c] : edges) cin >> a >> b >> c; vector<tuple<int, int, LL, int>> queries; REP (xd, q) { int type; cin >> type; if (type == 1) { int a, b, d; cin >> a >> b >> d; queries.emplace_back(type, a, b, d); } else if (type == 2) { int a, b; cin >> a >> b; queries.emplace_back(type, a, b, 0); } else if (type == 3) { int a; LL b; int c; cin >> a >> b >> c; queries.emplace_back(type, a, b, c); } else { int a; cin >> a; queries.emplace_back(type, a, 0, 0); } } bool is_first = m == (n - 1); for (auto [type, a, b, c] : queries) if (type == 1 || type == 2) is_first = false; if (is_first) { sub1(n, edges, queries); return 0; } bool is_second = true; const LL Z = 1'000'000'000'000'000ll; for (auto [type, a, b, c] : queries) if (type == 3 && b != Z) is_second = false; if (is_second) { sub2(n, edges, queries); return 0; } brute(n, edges, queries); } |