/tmp/solutions/build/dynamic_tree_subtree_add_subtree_sum-main.cpp:
    1|       |#include <common.h>
    2|       |prelude;
    3|       |
    4|       |namespace {
    5|       |
    6|       |constexpr int N = 200001;
    7|       |
    8|       |struct Node {
    9|       |  i64 sum;
   10|       |  i64 sum_;
   11|       |  i64 add;
   12|       |  int size;
   13|       |  int size_;
   14|       |  int p;
   15|       |  int ch[2];
   16|       |  i8 rev;
   17|       |  i8 k;
   18|       |} node[N];
   19|       |
   20|     35|def reverse(int x) -> void { node[x].rev ^= 1; }
   21|       |
   22|     39|def pushdown(int x) -> void {
   23|     39|  if (node[x].rev) {
  ------------------
  |  Branch (23:7): [True: 33.33%, False: 66.67%]
  ------------------
   24|     13|    std::swap(node[x].ch[0], node[x].ch[1]);
   25|     13|    node[node[x].ch[0]].k = 0;
   26|     13|    node[node[x].ch[1]].k = 1;
   27|     13|    reverse(node[x].ch[0]);
   28|     13|    reverse(node[x].ch[1]);
   29|     13|    node[x].rev = 0;
   30|     13|  }
   31|     39|}
   32|       |
   33|     45|def maintain(int x) -> void {
   34|     45|  node[x].size =
   35|     45|      node[node[x].ch[0]].size + 1 + node[node[x].ch[1]].size + node[x].size_;
   36|     45|  node[x].sum = node[node[x].ch[0]].sum + node[node[x].ch[1]].sum +
   37|     45|                node[x].sum_ + node[x].size * node[x].add;
   38|     45|}
   39|       |
   40|     16|def rotateup(int x) -> void {
   41|     16|  int p = node[x].p;
   42|     16|  int g = node[p].p;
   43|     16|  int k = node[x].k;
   44|     16|  int t = node[p].k;
   45|     16|  i64 b = node[x].add;
   46|     16|  i64 a = node[p].add;
   47|     16|  node[x].add = a + b;
   48|     16|  node[p].add = -b;
   49|     16|  node[node[x].ch[k ^ 1]].add += b;
   50|     16|  node[node[x].ch[k ^ 1]].sum += b * node[node[x].ch[k ^ 1]].size;
   51|     16|  node[node[x].ch[k ^ 1]].p = p;
   52|     16|  node[node[x].ch[k ^ 1]].k = k;
   53|     16|  node[p].ch[k] = node[x].ch[k ^ 1];
   54|     16|  node[p].p = x;
   55|     16|  node[p].k = k ^ 1;
   56|     16|  node[x].ch[k ^ 1] = p;
   57|     16|  node[x].p = g;
   58|     16|  node[x].k = t;
   59|     16|  if (t != -1) {
  ------------------
  |  Branch (59:7): [True: 0.00%, False: 100.00%]
  ------------------
   60|      0|    node[g].ch[t] = x;
   61|      0|  }
   62|     16|  maintain(p);
   63|     16|}
   64|       |
   65|     39|def is_root(int x) -> bool { return node[x].k == -1; }
   66|       |
   67|     29|def splay(int x) -> void {
   68|     29|  pushdown(x);
   69|     34|  while (!is_root(x)) {
  ------------------
  |  Branch (69:10): [True: 14.71%, False: 85.29%]
  ------------------
   70|      5|    if (int p = node[x].p; is_root(p)) {
  ------------------
  |  Branch (70:28): [True: 100.00%, False: 0.00%]
  ------------------
   71|      5|      pushdown(p);
   72|      5|      pushdown(x);
   73|      5|      rotateup(x);
   74|      5|    } else {
   75|      0|      int g = node[p].p;
   76|      0|      pushdown(g);
   77|      0|      pushdown(p);
   78|      0|      pushdown(x);
   79|      0|      if (node[x].k == node[p].k) {
  ------------------
  |  Branch (79:11): [True: 0.00%, False: 0.00%]
  ------------------
   80|      0|        rotateup(p);
   81|      0|        rotateup(x);
   82|      0|      } else {
   83|      0|        rotateup(x);
   84|      0|        rotateup(x);
   85|      0|      }
   86|      0|    }
   87|      5|  }
   88|     29|}
   89|       |
   90|     18|def access(int x) -> void {
   91|     18|  splay(x);
   92|     18|  node[node[x].ch[1]].k = -1;
   93|     18|  node[x].sum_ += node[node[x].ch[1]].sum;
   94|     18|  node[x].size_ += node[node[x].ch[1]].size;
   95|     18|  node[x].ch[1] = 0;
   96|     18|  maintain(x);
   97|     29|  while (int p = node[x].p) {
  ------------------
  |  Branch (97:14): [True: 37.93%, False: 62.07%]
  ------------------
   98|     11|    splay(p);
   99|     11|    node[node[p].ch[1]].k = -1;
  100|     11|    node[p].sum_ += node[node[p].ch[1]].sum;
  101|     11|    node[p].size_ += node[node[p].ch[1]].size;
  102|     11|    node[p].sum_ -= node[x].sum;
  103|     11|    node[p].size_ -= node[x].size;
  104|     11|    node[p].ch[1] = x;
  105|     11|    node[x].k = 1;
  106|     11|    rotateup(x);
  107|     11|    maintain(x);
  108|     11|  }
  109|     18|}
  110|       |
  111|       |int head[N];
  112|       |struct {
  113|       |  int to;
  114|       |  int next;
  115|       |} edge[N * 2];
  116|       |
  117|      5|def build(int u, int p) -> void {
  118|     13|  for (int e = head[u]; e; e = edge[e].next) {
                                         ^8
  ------------------
  |  Branch (118:25): [True: 61.54%, False: 38.46%]
  ------------------
  119|      8|    int v = edge[e].to;
  120|      8|    if (v != p) {
  ------------------
  |  Branch (120:9): [True: 50.00%, False: 50.00%]
  ------------------
  121|      4|      build(v, u);
  122|      4|      node[v + 1].p = u + 1;
  123|      4|      node[u + 1].sum_ += node[v + 1].sum;
  124|      4|      node[u + 1].size_ += node[v + 1].size;
  125|      4|    }
  126|      8|  }
  127|      5|  node[u + 1].sum = node[u + 1].sum_;
  128|      5|  node[u + 1].size = node[u + 1].size_ + 1;
  129|      5|}
  130|       |
  131|       |} // namespace
  132|       |
  133|      1|int main() {
  134|      1|  rd rd;
  135|      1|  wt wt;
  136|      1|  int n = rd.uh();
  137|      1|  int q = rd.uh();
  138|      1|#ifdef LOCAL
  139|      1|  std::memset(head, 0, 4 * n);
  140|      1|#endif
  141|      6|  for (int i = 1; i <= n; ++i) node[i] = {.sum_ = rd.uw(), .k = -1};
                                        ^5   ^5
  ------------------
  |  Branch (141:19): [True: 83.33%, False: 16.67%]
  ------------------
  142|      5|  for (int i = 1; i != n; ++i) {
                                        ^4
  ------------------
  |  Branch (142:19): [True: 80.00%, False: 20.00%]
  ------------------
  143|      4|    int u = rd.uh();
  144|      4|    int v = rd.uh();
  145|      4|    edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
  146|      4|    edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
  147|      4|  }
  148|      1|  build(0, 0);
  149|      8|  while (q--) {
  ------------------
  |  Branch (149:10): [True: 87.50%, False: 12.50%]
  ------------------
  150|      7|    let t = rd.u1();
  151|      7|    if (t == 0) {
  ------------------
  |  Branch (151:9): [True: 28.57%, False: 71.43%]
  ------------------
  152|      2|      int u = rd.uh() + 1;
  153|      2|      int v = rd.uh() + 1;
  154|      2|      access(u);
  155|      2|      reverse(u);
  156|      2|      access(v);
  157|      2|      node[node[v].ch[0]].p = 0;
  158|      2|      node[node[v].ch[0]].k = -1;
  159|      2|      node[node[v].ch[0]].add += node[v].add;
  160|      2|      node[v].ch[0] = 0;
  161|      2|      int w = rd.uh() + 1;
  162|      2|      int x = rd.uh() + 1;
  163|      2|      access(w);
  164|      2|      reverse(w);
  165|      2|      access(x);
  166|      2|      node[w].p = x;
  167|      2|      node[w].add -= node[x].add;
  168|      2|      node[w].sum -= node[x].add * node[w].size;
  169|      2|      node[x].sum_ += node[w].sum;
  170|      2|      node[x].size_ += node[w].size;
  171|      2|    }
  172|      7|    if (t == 1) {
  ------------------
  |  Branch (172:9): [True: 14.29%, False: 85.71%]
  ------------------
  173|      1|      int u = rd.uh() + 1;
  174|      1|      int p = rd.uh() + 1;
  175|      1|      i64 x = rd.uw();
  176|      1|      access(u);
  177|      1|      reverse(u);
  178|      1|      access(p);
  179|      1|      node[u].add += x;
  180|      1|      node[u].sum += x * node[u].size;
  181|      1|    }
  182|      7|    if (t == 2) {
  ------------------
  |  Branch (182:9): [True: 57.14%, False: 42.86%]
  ------------------
  183|      4|      int u = rd.uh() + 1;
  184|      4|      int p = rd.uh() + 1;
  185|      4|      access(u);
  186|      4|      reverse(u);
  187|      4|      access(p);
  188|      4|      i64 ans = node[u].sum + node[u].size * node[p].add;
  189|      4|      wt.ud(ans);
  190|      4|    }
  191|      7|  }
  192|      1|  return 0;
  193|      1|}