/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|  3.49k|def reverse(int x) -> void { node[x].rev ^= 1; }
   21|       |
   22|  4.72k|def pushdown(int x) -> void {
   23|  4.72k|  if (node[x].rev) {
  ------------------
  |  Branch (23:7): [True: 33.50%, False: 66.50%]
  ------------------
   24|  1.58k|    std::swap(node[x].ch[0], node[x].ch[1]);
   25|  1.58k|    node[node[x].ch[0]].k = 0;
   26|  1.58k|    node[node[x].ch[1]].k = 1;
   27|  1.58k|    reverse(node[x].ch[0]);
   28|  1.58k|    reverse(node[x].ch[1]);
   29|  1.58k|    node[x].rev = 0;
   30|  1.58k|  }
   31|  4.72k|}
   32|       |
   33|  4.77k|def maintain(int x) -> void {
   34|  4.77k|  node[x].size =
   35|  4.77k|      node[node[x].ch[0]].size + 1 + node[node[x].ch[1]].size + node[x].size_;
   36|  4.77k|  node[x].sum = node[node[x].ch[0]].sum + node[node[x].ch[1]].sum +
   37|  4.77k|                node[x].sum_ + node[x].size * node[x].add;
   38|  4.77k|}
   39|       |
   40|  2.93k|def rotateup(int x) -> void {
   41|  2.93k|  int p = node[x].p;
   42|  2.93k|  int g = node[p].p;
   43|  2.93k|  int k = node[x].k;
   44|  2.93k|  int t = node[p].k;
   45|  2.93k|  i64 b = node[x].add;
   46|  2.93k|  i64 a = node[p].add;
   47|  2.93k|  node[x].add = a + b;
   48|  2.93k|  node[p].add = -b;
   49|  2.93k|  node[node[x].ch[k ^ 1]].add += b;
   50|  2.93k|  node[node[x].ch[k ^ 1]].sum += b * node[node[x].ch[k ^ 1]].size;
   51|  2.93k|  node[node[x].ch[k ^ 1]].p = p;
   52|  2.93k|  node[node[x].ch[k ^ 1]].k = k;
   53|  2.93k|  node[p].ch[k] = node[x].ch[k ^ 1];
   54|  2.93k|  node[p].p = x;
   55|  2.93k|  node[p].k = k ^ 1;
   56|  2.93k|  node[x].ch[k ^ 1] = p;
   57|  2.93k|  node[x].p = g;
   58|  2.93k|  node[x].k = t;
   59|  2.93k|  if (t != -1) {
  ------------------
  |  Branch (59:7): [True: 29.75%, False: 70.25%]
  ------------------
   60|    873|    node[g].ch[t] = x;
   61|    873|  }
   62|  2.93k|  maintain(p);
   63|  2.93k|}
   64|       |
   65|  4.10k|def is_root(int x) -> bool { return node[x].k == -1; }
   66|       |
   67|  1.83k|def splay(int x) -> void {
   68|  1.83k|  pushdown(x);
   69|  2.97k|  while (!is_root(x)) {
  ------------------
  |  Branch (69:10): [True: 38.21%, False: 61.79%]
  ------------------
   70|  1.13k|    if (int p = node[x].p; is_root(p)) {
  ------------------
  |  Branch (70:28): [True: 45.51%, False: 54.49%]
  ------------------
   71|    517|      pushdown(p);
   72|    517|      pushdown(x);
   73|    517|      rotateup(x);
   74|    619|    } else {
   75|    619|      int g = node[p].p;
   76|    619|      pushdown(g);
   77|    619|      pushdown(p);
   78|    619|      pushdown(x);
   79|    619|      if (node[x].k == node[p].k) {
  ------------------
  |  Branch (79:11): [True: 52.18%, False: 47.82%]
  ------------------
   80|    323|        rotateup(p);
   81|    323|        rotateup(x);
   82|    323|      } else {
   83|    296|        rotateup(x);
   84|    296|        rotateup(x);
   85|    296|      }
   86|    619|    }
   87|  1.13k|  }
   88|  1.83k|}
   89|       |
   90|    658|def access(int x) -> void {
   91|    658|  splay(x);
   92|    658|  node[node[x].ch[1]].k = -1;
   93|    658|  node[x].sum_ += node[node[x].ch[1]].sum;
   94|    658|  node[x].size_ += node[node[x].ch[1]].size;
   95|    658|  node[x].ch[1] = 0;
   96|    658|  maintain(x);
   97|  1.83k|  while (int p = node[x].p) {
  ------------------
  |  Branch (97:14): [True: 64.18%, False: 35.82%]
  ------------------
   98|  1.17k|    splay(p);
   99|  1.17k|    node[node[p].ch[1]].k = -1;
  100|  1.17k|    node[p].sum_ += node[node[p].ch[1]].sum;
  101|  1.17k|    node[p].size_ += node[node[p].ch[1]].size;
  102|  1.17k|    node[p].sum_ -= node[x].sum;
  103|  1.17k|    node[p].size_ -= node[x].size;
  104|  1.17k|    node[p].ch[1] = x;
  105|  1.17k|    node[x].k = 1;
  106|  1.17k|    rotateup(x);
  107|  1.17k|    maintain(x);
  108|  1.17k|  }
  109|    658|}
  110|       |
  111|       |int head[N];
  112|       |struct {
  113|       |  int to;
  114|       |  int next;
  115|       |} edge[N * 2];
  116|       |
  117|    194|def build(int u, int p) -> void {
  118|    580|  for (int e = head[u]; e; e = edge[e].next) {
                                         ^386
  ------------------
  |  Branch (118:25): [True: 66.55%, False: 33.45%]
  ------------------
  119|    386|    int v = edge[e].to;
  120|    386|    if (v != p) {
  ------------------
  |  Branch (120:9): [True: 50.00%, False: 50.00%]
  ------------------
  121|    193|      build(v, u);
  122|    193|      node[v + 1].p = u + 1;
  123|    193|      node[u + 1].sum_ += node[v + 1].sum;
  124|    193|      node[u + 1].size_ += node[v + 1].size;
  125|    193|    }
  126|    386|  }
  127|    194|  node[u + 1].sum = node[u + 1].sum_;
  128|    194|  node[u + 1].size = node[u + 1].size_ + 1;
  129|    194|}
  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|    195|  for (int i = 1; i <= n; ++i) node[i] = {.sum_ = rd.uw(), .k = -1};
                                        ^194 ^194
  ------------------
  |  Branch (141:19): [True: 99.49%, False: 0.51%]
  ------------------
  142|    194|  for (int i = 1; i != n; ++i) {
                                        ^193
  ------------------
  |  Branch (142:19): [True: 99.48%, False: 0.52%]
  ------------------
  143|    193|    int u = rd.uh();
  144|    193|    int v = rd.uh();
  145|    193|    edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
  146|    193|    edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
  147|    193|  }
  148|      1|  build(0, 0);
  149|    240|  while (q--) {
  ------------------
  |  Branch (149:10): [True: 99.58%, False: 0.42%]
  ------------------
  150|    239|    let t = rd.u1();
  151|    239|    if (t == 0) {
  ------------------
  |  Branch (151:9): [True: 37.66%, False: 62.34%]
  ------------------
  152|     90|      int u = rd.uh() + 1;
  153|     90|      int v = rd.uh() + 1;
  154|     90|      access(u);
  155|     90|      reverse(u);
  156|     90|      access(v);
  157|     90|      node[node[v].ch[0]].p = 0;
  158|     90|      node[node[v].ch[0]].k = -1;
  159|     90|      node[node[v].ch[0]].add += node[v].add;
  160|     90|      node[v].ch[0] = 0;
  161|     90|      int w = rd.uh() + 1;
  162|     90|      int x = rd.uh() + 1;
  163|     90|      access(w);
  164|     90|      reverse(w);
  165|     90|      access(x);
  166|     90|      node[w].p = x;
  167|     90|      node[w].add -= node[x].add;
  168|     90|      node[w].sum -= node[x].add * node[w].size;
  169|     90|      node[x].sum_ += node[w].sum;
  170|     90|      node[x].size_ += node[w].size;
  171|     90|    }
  172|    239|    if (t == 1) {
  ------------------
  |  Branch (172:9): [True: 32.22%, False: 67.78%]
  ------------------
  173|     77|      int u = rd.uh() + 1;
  174|     77|      int p = rd.uh() + 1;
  175|     77|      i64 x = rd.uw();
  176|     77|      access(u);
  177|     77|      reverse(u);
  178|     77|      access(p);
  179|     77|      node[u].add += x;
  180|     77|      node[u].sum += x * node[u].size;
  181|     77|    }
  182|    239|    if (t == 2) {
  ------------------
  |  Branch (182:9): [True: 30.13%, False: 69.87%]
  ------------------
  183|     72|      int u = rd.uh() + 1;
  184|     72|      int p = rd.uh() + 1;
  185|     72|      access(u);
  186|     72|      reverse(u);
  187|     72|      access(p);
  188|     72|      i64 ans = node[u].sum + node[u].size * node[p].add;
  189|     72|      wt.ud(ans);
  190|     72|    }
  191|    239|  }
  192|      1|  return 0;
  193|      1|}