- 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
- 53 lines of C++ from the credited upstream file yosupo-dynamic-tree-vertex-add-subtree-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_subtree_sum"2 3#include "../../template/template.hpp"45#include "../../lct/link-cut-tree-subtree.hpp"6 7using namespace Nyaan;8 9using T = ll;10T f(ll a, ll b) { return a + b; }11T finv(ll a, ll b) { return a - b; }12 13#include "../../misc/fastio.hpp"14void Nyaan::solve() {15 using LCT = LinkCutTreeSubtreeQuery<T, f, finv>;16 LCT lct;17 18 int N, Q;19 rd(N, Q);20 21 vector<LCT::Ptr> vs(N);22 rep(i, N) {23 int a;24 rd(a);25 vs[i] = lct.my_new(a);26 }27 28 for (int i = 1; i < N; i++) {29 int a, b;30 rd(a, b);31 lct.link(vs[a], vs[b]);32 }33 while (Q--) {34 int cmd;35 rd(cmd);36 if (cmd == 0) {37 int u, v, w, x;38 rd(u, v, w, x);39 lct.cut(vs[u], vs[v]);40 lct.link(vs[w], vs[x]);41 } else if (cmd == 1) {42 int u, x;43 rd(u, x);44 lct.set_key(vs[u], lct.get_key(vs[u]) + x);45 } else {46 int v, p;47 rd(v, p);48 lct.evert(vs[p]);49 wtn(lct.subtree(vs[v]));50 }51 }52}53