/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.47k|def reverse(int x) -> void { node[x].rev ^= 1; }
   21|       |
   22|  4.67k|def pushdown(int x) -> void {
   23|  4.67k|  if (node[x].rev) {
  ------------------
  |  Branch (23:7): [True: 33.81%, False: 66.19%]
  ------------------
   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.67k|}
   32|       |
   33|  4.66k|def maintain(int x) -> void {
   34|  4.66k|  node[x].size =
   35|  4.66k|      node[node[x].ch[0]].size + 1 + node[node[x].ch[1]].size + node[x].size_;
   36|  4.66k|  node[x].sum = node[node[x].ch[0]].sum + node[node[x].ch[1]].sum +
   37|  4.66k|                node[x].sum_ + node[x].size * node[x].add;
   38|  4.66k|}
   39|       |
   40|  2.90k|def rotateup(int x) -> void {
   41|  2.90k|  int p = node[x].p;
   42|  2.90k|  int g = node[p].p;
   43|  2.90k|  int k = node[x].k;
   44|  2.90k|  int t = node[p].k;
   45|  2.90k|  i64 b = node[x].add;
   46|  2.90k|  i64 a = node[p].add;
   47|  2.90k|  node[x].add = a + b;
   48|  2.90k|  node[p].add = -b;
   49|  2.90k|  node[node[x].ch[k ^ 1]].add += b;
   50|  2.90k|  node[node[x].ch[k ^ 1]].sum += b * node[node[x].ch[k ^ 1]].size;
   51|  2.90k|  node[node[x].ch[k ^ 1]].p = p;
   52|  2.90k|  node[node[x].ch[k ^ 1]].k = k;
   53|  2.90k|  node[p].ch[k] = node[x].ch[k ^ 1];
   54|  2.90k|  node[p].p = x;
   55|  2.90k|  node[p].k = k ^ 1;
   56|  2.90k|  node[x].ch[k ^ 1] = p;
   57|  2.90k|  node[x].p = g;
   58|  2.90k|  node[x].k = t;
   59|  2.90k|  if (t != -1) {
  ------------------
  |  Branch (59:7): [True: 30.67%, False: 69.33%]
  ------------------
   60|    892|    node[g].ch[t] = x;
   61|    892|  }
   62|  2.90k|  maintain(p);
   63|  2.90k|}
   64|       |
   65|  4.05k|def is_root(int x) -> bool { return node[x].k == -1; }
   66|       |
   67|  1.76k|def splay(int x) -> void {
   68|  1.76k|  pushdown(x);
   69|  2.90k|  while (!is_root(x)) {
  ------------------
  |  Branch (69:10): [True: 39.42%, False: 60.58%]
  ------------------
   70|  1.14k|    if (int p = node[x].p; is_root(p)) {
  ------------------
  |  Branch (70:28): [True: 45.64%, False: 54.36%]
  ------------------
   71|    523|      pushdown(p);
   72|    523|      pushdown(x);
   73|    523|      rotateup(x);
   74|    623|    } else {
   75|    623|      int g = node[p].p;
   76|    623|      pushdown(g);
   77|    623|      pushdown(p);
   78|    623|      pushdown(x);
   79|    623|      if (node[x].k == node[p].k) {
  ------------------
  |  Branch (79:11): [True: 53.45%, False: 46.55%]
  ------------------
   80|    333|        rotateup(p);
   81|    333|        rotateup(x);
   82|    333|      } else {
   83|    290|        rotateup(x);
   84|    290|        rotateup(x);
   85|    290|      }
   86|    623|    }
   87|  1.14k|  }
   88|  1.76k|}
   89|       |
   90|    622|def access(int x) -> void {
   91|    622|  splay(x);
   92|    622|  node[node[x].ch[1]].k = -1;
   93|    622|  node[x].sum_ += node[node[x].ch[1]].sum;
   94|    622|  node[x].size_ += node[node[x].ch[1]].size;
   95|    622|  node[x].ch[1] = 0;
   96|    622|  maintain(x);
   97|  1.76k|  while (int p = node[x].p) {
  ------------------
  |  Branch (97:14): [True: 64.68%, False: 35.32%]
  ------------------
   98|  1.13k|    splay(p);
   99|  1.13k|    node[node[p].ch[1]].k = -1;
  100|  1.13k|    node[p].sum_ += node[node[p].ch[1]].sum;
  101|  1.13k|    node[p].size_ += node[node[p].ch[1]].size;
  102|  1.13k|    node[p].sum_ -= node[x].sum;
  103|  1.13k|    node[p].size_ -= node[x].size;
  104|  1.13k|    node[p].ch[1] = x;
  105|  1.13k|    node[x].k = 1;
  106|  1.13k|    rotateup(x);
  107|  1.13k|    maintain(x);
  108|  1.13k|  }
  109|    622|}
  110|       |
  111|       |int head[N];
  112|       |struct {
  113|       |  int to;
  114|       |  int next;
  115|       |} edge[N * 2];
  116|       |
  117|    199|def build(int u, int p) -> void {
  118|    595|  for (int e = head[u]; e; e = edge[e].next) {
                                         ^396
  ------------------
  |  Branch (118:25): [True: 66.55%, False: 33.45%]
  ------------------
  119|    396|    int v = edge[e].to;
  120|    396|    if (v != p) {
  ------------------
  |  Branch (120:9): [True: 50.00%, False: 50.00%]
  ------------------
  121|    198|      build(v, u);
  122|    198|      node[v + 1].p = u + 1;
  123|    198|      node[u + 1].sum_ += node[v + 1].sum;
  124|    198|      node[u + 1].size_ += node[v + 1].size;
  125|    198|    }
  126|    396|  }
  127|    199|  node[u + 1].sum = node[u + 1].sum_;
  128|    199|  node[u + 1].size = node[u + 1].size_ + 1;
  129|    199|}
  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|    200|  for (int i = 1; i <= n; ++i) node[i] = {.sum_ = rd.uw(), .k = -1};
                                        ^199 ^199
  ------------------
  |  Branch (141:19): [True: 99.50%, False: 0.50%]
  ------------------
  142|    199|  for (int i = 1; i != n; ++i) {
                                        ^198
  ------------------
  |  Branch (142:19): [True: 99.50%, False: 0.50%]
  ------------------
  143|    198|    int u = rd.uh();
  144|    198|    int v = rd.uh();
  145|    198|    edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
  146|    198|    edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
  147|    198|  }
  148|      1|  build(0, 0);
  149|    236|  while (q--) {
  ------------------
  |  Branch (149:10): [True: 99.58%, False: 0.42%]
  ------------------
  150|    235|    let t = rd.u1();
  151|    235|    if (t == 0) {
  ------------------
  |  Branch (151:9): [True: 32.34%, False: 67.66%]
  ------------------
  152|     76|      int u = rd.uh() + 1;
  153|     76|      int v = rd.uh() + 1;
  154|     76|      access(u);
  155|     76|      reverse(u);
  156|     76|      access(v);
  157|     76|      node[node[v].ch[0]].p = 0;
  158|     76|      node[node[v].ch[0]].k = -1;
  159|     76|      node[node[v].ch[0]].add += node[v].add;
  160|     76|      node[v].ch[0] = 0;
  161|     76|      int w = rd.uh() + 1;
  162|     76|      int x = rd.uh() + 1;
  163|     76|      access(w);
  164|     76|      reverse(w);
  165|     76|      access(x);
  166|     76|      node[w].p = x;
  167|     76|      node[w].add -= node[x].add;
  168|     76|      node[w].sum -= node[x].add * node[w].size;
  169|     76|      node[x].sum_ += node[w].sum;
  170|     76|      node[x].size_ += node[w].size;
  171|     76|    }
  172|    235|    if (t == 1) {
  ------------------
  |  Branch (172:9): [True: 35.74%, False: 64.26%]
  ------------------
  173|     84|      int u = rd.uh() + 1;
  174|     84|      int p = rd.uh() + 1;
  175|     84|      i64 x = rd.uw();
  176|     84|      access(u);
  177|     84|      reverse(u);
  178|     84|      access(p);
  179|     84|      node[u].add += x;
  180|     84|      node[u].sum += x * node[u].size;
  181|     84|    }
  182|    235|    if (t == 2) {
  ------------------
  |  Branch (182:9): [True: 31.91%, False: 68.09%]
  ------------------
  183|     75|      int u = rd.uh() + 1;
  184|     75|      int p = rd.uh() + 1;
  185|     75|      access(u);
  186|     75|      reverse(u);
  187|     75|      access(p);
  188|     75|      i64 ans = node[u].sum + node[u].size * node[p].add;
  189|     75|      wt.ud(ans);
  190|     75|    }
  191|    235|  }
  192|      1|  return 0;
  193|      1|}