/tmp/solutions/build/dynamic_tree_vertex_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|       |  u64 val;
   10|       |  u64 sum;
   11|       |  u64 sum_;
   12|       |  int p;
   13|       |  int ch[2];
   14|       |  i8 rev;
   15|       |  i8 k;
   16|       |} node[N];
   17|       |
   18|  13.2k|def reverse(int x) -> void { node[x].rev ^= 1; }
   19|       |
   20|  21.3k|def pushdown(int x) -> void {
   21|  21.3k|  if (node[x].rev) {
  ------------------
  |  Branch (21:7): [True: 28.86%, False: 71.14%]
  ------------------
   22|  6.15k|    std::swap(node[x].ch[0], node[x].ch[1]);
   23|  6.15k|    node[node[x].ch[0]].k = 0;
   24|  6.15k|    node[node[x].ch[1]].k = 1;
   25|  6.15k|    reverse(node[x].ch[0]);
   26|  6.15k|    reverse(node[x].ch[1]);
   27|  6.15k|    node[x].rev = 0;
   28|  6.15k|  }
   29|  21.3k|}
   30|       |
   31|  21.1k|def maintain(int x) -> void {
   32|  21.1k|  node[x].sum = node[node[x].ch[0]].sum + node[x].val +
   33|  21.1k|                node[node[x].ch[1]].sum + node[x].sum_;
   34|  21.1k|}
   35|       |
   36|  13.7k|def rotateup(int x) -> void {
   37|  13.7k|  int p = node[x].p;
   38|  13.7k|  int g = node[p].p;
   39|  13.7k|  int k = node[x].k;
   40|  13.7k|  int t = node[p].k;
   41|  13.7k|  node[node[x].ch[k ^ 1]].p = p;
   42|  13.7k|  node[node[x].ch[k ^ 1]].k = k;
   43|  13.7k|  node[p].ch[k] = node[x].ch[k ^ 1];
   44|  13.7k|  node[p].p = x;
   45|  13.7k|  node[p].k = k ^ 1;
   46|  13.7k|  node[x].ch[k ^ 1] = p;
   47|  13.7k|  node[x].p = g;
   48|  13.7k|  node[x].k = t;
   49|  13.7k|  if (t != -1) {
  ------------------
  |  Branch (49:7): [True: 32.32%, False: 67.68%]
  ------------------
   50|  4.43k|    node[g].ch[t] = x;
   51|  4.43k|  }
   52|  13.7k|  maintain(p);
   53|  13.7k|}
   54|       |
   55|  18.2k|def is_root(int x) -> bool { return node[x].k == -1; }
   56|       |
   57|  7.40k|def splay(int x) -> void {
   58|  7.40k|  pushdown(x);
   59|  12.8k|  while (!is_root(x)) {
  ------------------
  |  Branch (59:10): [True: 42.16%, False: 57.84%]
  ------------------
   60|  5.40k|    if (int p = node[x].p; is_root(p)) {
  ------------------
  |  Branch (60:28): [True: 41.96%, False: 58.04%]
  ------------------
   61|  2.26k|      pushdown(p);
   62|  2.26k|      pushdown(x);
   63|  2.26k|      rotateup(x);
   64|  3.13k|    } else {
   65|  3.13k|      int g = node[p].p;
   66|  3.13k|      pushdown(g);
   67|  3.13k|      pushdown(p);
   68|  3.13k|      pushdown(x);
   69|  3.13k|      if (node[x].k == node[p].k) {
  ------------------
  |  Branch (69:11): [True: 47.54%, False: 52.46%]
  ------------------
   70|  1.49k|        rotateup(p);
   71|  1.49k|        rotateup(x);
   72|  1.64k|      } else {
   73|  1.64k|        rotateup(x);
   74|  1.64k|        rotateup(x);
   75|  1.64k|      }
   76|  3.13k|    }
   77|  5.40k|  }
   78|  7.40k|}
   79|       |
   80|  2.21k|def access(int x) -> void {
   81|  2.21k|  splay(x);
   82|  2.21k|  node[node[x].ch[1]].k = -1;
   83|  2.21k|  node[x].sum_ += node[node[x].ch[1]].sum;
   84|  2.21k|  node[x].ch[1] = 0;
   85|  2.21k|  maintain(x);
   86|  7.40k|  while (int p = node[x].p) {
  ------------------
  |  Branch (86:14): [True: 70.06%, False: 29.94%]
  ------------------
   87|  5.18k|    splay(p);
   88|  5.18k|    node[node[p].ch[1]].k = -1;
   89|  5.18k|    node[p].sum_ += node[node[p].ch[1]].sum;
   90|  5.18k|    node[p].sum_ -= node[x].sum;
   91|  5.18k|    node[p].ch[1] = x;
   92|  5.18k|    node[x].k = 1;
   93|  5.18k|    rotateup(x);
   94|  5.18k|    maintain(x);
   95|  5.18k|  }
   96|  2.21k|}
   97|       |
   98|       |int head[N];
   99|       |struct {
  100|       |  int to;
  101|       |  int next;
  102|       |} edge[N * 2];
  103|       |
  104|    952|def build(int u, int p) -> void {
  105|  2.85k|  for (int e = head[u]; e; e = edge[e].next) {
                                         ^1.90k
  ------------------
  |  Branch (105:25): [True: 66.64%, False: 33.36%]
  ------------------
  106|  1.90k|    int v = edge[e].to;
  107|  1.90k|    if (v != p) {
  ------------------
  |  Branch (107:9): [True: 50.00%, False: 50.00%]
  ------------------
  108|    951|      build(v, u);
  109|    951|      node[v + 1].p = u + 1;
  110|    951|      node[u + 1].sum_ += node[v + 1].sum;
  111|    951|    }
  112|  1.90k|  }
  113|    952|  node[u + 1].sum = node[u + 1].val + node[u + 1].sum_;
  114|    952|}
  115|       |
  116|       |} // namespace
  117|       |
  118|      1|int main() {
  119|      1|  rd rd;
  120|      1|  wt wt;
  121|      1|  int n = rd.uh();
  122|      1|  int q = rd.uh();
  123|      1|#ifdef LOCAL
  124|      1|  std::memset(head, 0, 4 * n);
  125|      1|#endif
  126|    953|  for (int i = 1; i <= n; ++i) node[i] = {.val = rd.uw(), .k = -1};
                                        ^952 ^952
  ------------------
  |  Branch (126:19): [True: 99.90%, False: 0.10%]
  ------------------
  127|    952|  for (int i = 1; i != n; ++i) {
                                        ^951
  ------------------
  |  Branch (127:19): [True: 99.89%, False: 0.11%]
  ------------------
  128|    951|    int u = rd.uh();
  129|    951|    int v = rd.uh();
  130|    951|    edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
  131|    951|    edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
  132|    951|  }
  133|      1|  build(0, 0);
  134|    943|  while (q--) {
  ------------------
  |  Branch (134:10): [True: 99.89%, False: 0.11%]
  ------------------
  135|    942|    let t = rd.u1();
  136|    942|    if (t == 0) {
  ------------------
  |  Branch (136:9): [True: 33.33%, False: 66.67%]
  ------------------
  137|    314|      int u = rd.uh() + 1;
  138|    314|      int v = rd.uh() + 1;
  139|    314|      access(u);
  140|    314|      reverse(u);
  141|    314|      access(v);
  142|    314|      node[node[v].ch[0]].p = 0;
  143|    314|      node[node[v].ch[0]].k = -1;
  144|    314|      node[v].ch[0] = 0;
  145|    314|      int w = rd.uh() + 1;
  146|    314|      int x = rd.uh() + 1;
  147|    314|      access(w);
  148|    314|      reverse(w);
  149|    314|      access(x);
  150|    314|      node[w].p = x;
  151|    314|      node[x].sum_ += node[w].sum;
  152|    314|    }
  153|    942|    if (t == 1) {
  ------------------
  |  Branch (153:9): [True: 31.21%, False: 68.79%]
  ------------------
  154|    294|      int u = rd.uh() + 1;
  155|    294|      u32 x = rd.uw();
  156|    294|      access(u);
  157|    294|      node[u].val += x;
  158|    294|    }
  159|    942|    if (t == 2) {
  ------------------
  |  Branch (159:9): [True: 35.46%, False: 64.54%]
  ------------------
  160|    334|      int u = rd.uh() + 1;
  161|    334|      int p = rd.uh() + 1;
  162|    334|      access(u);
  163|    334|      reverse(u);
  164|    334|      access(p);
  165|    334|      wt.ud(node[u].sum);
  166|    334|    }
  167|    942|  }
  168|      1|  return 0;
  169|      1|}