- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 61 lines of C++ from the credited upstream file yosupo-dynamic-tree-vertex-add-path-sum.test.cpp.
- The implementation visibly relies on sequence storage.
- 2 loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1#define PROBLEM "https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_path_sum"2 3#include "../../template/template.hpp"45 6using namespace Nyaan;7 8#include "../../modint/montgomery-modint.hpp"9using mint = LazyMontgomeryModInt<998244353>;10using vm = vector<mint>;11#include "../../math/affine-transformation.hpp"12#include "../../misc/fastio.hpp"1314#include "../../lct/link-cut-tree.hpp"1516 17using T = long long;18T f(T a, T b) { return a + b; }19T ts(T a) { return a; }20 21using namespace Nyaan;22void Nyaan::solve() {23 int N, Q;24 rd(N, Q);25 26 using LCT = LinkCutTree<T, f, ts>;27 LCT lct;28 29 vector<LCT::Ptr> vs(N);30 rep(i, N) {31 int a;32 rd(a);33 vs[i] = lct.my_new(a);34 }35 36 for (int i = 1; i < N; i++) {37 int a, b;38 rd(a, b);39 lct.link(vs[a], vs[b]);40 }41 while (Q--) {42 int cmd;43 rd(cmd);44 if (cmd == 0) {45 int u, v, w, x;46 rd(u, v, w, x);47 lct.cut(vs[u], vs[v]);48 lct.link(vs[w], vs[x]);49 } else if (cmd == 1) {50 int u, x;51 rd(u, x);52 lct.set_key(vs[u], lct.get_key(vs[u]) + x);53 } else {54 int u, v;55 rd(u, v);56 T fold = lct.fold(vs[u], vs[v]);57 wtn(fold);58 }59 }60}61