/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|  2.31M|def reverse(int x) -> void { node[x].rev ^= 1; }
   19|       |
   20|  5.54M|def pushdown(int x) -> void {
   21|  5.54M|  if (node[x].rev) {
  ------------------
  |  Branch (21:7): [True: 19.58%, False: 80.42%]
  ------------------
   22|  1.08M|    std::swap(node[x].ch[0], node[x].ch[1]);
   23|  1.08M|    node[node[x].ch[0]].k = 0;
   24|  1.08M|    node[node[x].ch[1]].k = 1;
   25|  1.08M|    reverse(node[x].ch[0]);
   26|  1.08M|    reverse(node[x].ch[1]);
   27|  1.08M|    node[x].rev = 0;
   28|  1.08M|  }
   29|  5.54M|}
   30|       |
   31|  5.33M|def maintain(int x) -> void {
   32|  5.33M|  node[x].sum = node[node[x].ch[0]].sum + node[x].val +
   33|  5.33M|                node[node[x].ch[1]].sum + node[x].sum_;
   34|  5.33M|}
   35|       |
   36|  3.69M|def rotateup(int x) -> void {
   37|  3.69M|  int p = node[x].p;
   38|  3.69M|  int g = node[p].p;
   39|  3.69M|  int k = node[x].k;
   40|  3.69M|  int t = node[p].k;
   41|  3.69M|  node[node[x].ch[k ^ 1]].p = p;
   42|  3.69M|  node[node[x].ch[k ^ 1]].k = k;
   43|  3.69M|  node[p].ch[k] = node[x].ch[k ^ 1];
   44|  3.69M|  node[p].p = x;
   45|  3.69M|  node[p].k = k ^ 1;
   46|  3.69M|  node[x].ch[k ^ 1] = p;
   47|  3.69M|  node[x].p = g;
   48|  3.69M|  node[x].k = t;
   49|  3.69M|  if (t != -1) {
  ------------------
  |  Branch (49:7): [True: 33.12%, False: 66.88%]
  ------------------
   50|  1.22M|    node[g].ch[t] = x;
   51|  1.22M|  }
   52|  3.69M|  maintain(p);
   53|  3.69M|}
   54|       |
   55|  4.64M|def is_root(int x) -> bool { return node[x].k == -1; }
   56|       |
   57|  1.64M|def splay(int x) -> void {
   58|  1.64M|  pushdown(x);
   59|  3.14M|  while (!is_root(x)) {
  ------------------
  |  Branch (59:10): [True: 47.74%, False: 52.26%]
  ------------------
   60|  1.50M|    if (int p = node[x].p; is_root(p)) {
  ------------------
  |  Branch (60:28): [True: 40.59%, False: 59.41%]
  ------------------
   61|   609k|      pushdown(p);
   62|   609k|      pushdown(x);
   63|   609k|      rotateup(x);
   64|   892k|    } else {
   65|   892k|      int g = node[p].p;
   66|   892k|      pushdown(g);
   67|   892k|      pushdown(p);
   68|   892k|      pushdown(x);
   69|   892k|      if (node[x].k == node[p].k) {
  ------------------
  |  Branch (69:11): [True: 51.09%, False: 48.91%]
  ------------------
   70|   455k|        rotateup(p);
   71|   455k|        rotateup(x);
   72|   455k|      } else {
   73|   436k|        rotateup(x);
   74|   436k|        rotateup(x);
   75|   436k|      }
   76|   892k|    }
   77|  1.50M|  }
   78|  1.64M|}
   79|       |
   80|   347k|def access(int x) -> void {
   81|   347k|  splay(x);
   82|   347k|  node[node[x].ch[1]].k = -1;
   83|   347k|  node[x].sum_ += node[node[x].ch[1]].sum;
   84|   347k|  node[x].ch[1] = 0;
   85|   347k|  maintain(x);
   86|  1.64M|  while (int p = node[x].p) {
  ------------------
  |  Branch (86:14): [True: 78.84%, False: 21.16%]
  ------------------
   87|  1.29M|    splay(p);
   88|  1.29M|    node[node[p].ch[1]].k = -1;
   89|  1.29M|    node[p].sum_ += node[node[p].ch[1]].sum;
   90|  1.29M|    node[p].sum_ -= node[x].sum;
   91|  1.29M|    node[p].ch[1] = x;
   92|  1.29M|    node[x].k = 1;
   93|  1.29M|    rotateup(x);
   94|  1.29M|    maintain(x);
   95|  1.29M|  }
   96|   347k|}
   97|       |
   98|       |int head[N];
   99|       |struct {
  100|       |  int to;
  101|       |  int next;
  102|       |} edge[N * 2];
  103|       |
  104|   127k|def build(int u, int p) -> void {
  105|   383k|  for (int e = head[u]; e; e = edge[e].next) {
                                         ^255k
  ------------------
  |  Branch (105:25): [True: 66.67%, False: 33.33%]
  ------------------
  106|   255k|    int v = edge[e].to;
  107|   255k|    if (v != p) {
  ------------------
  |  Branch (107:9): [True: 50.00%, False: 50.00%]
  ------------------
  108|   127k|      build(v, u);
  109|   127k|      node[v + 1].p = u + 1;
  110|   127k|      node[u + 1].sum_ += node[v + 1].sum;
  111|   127k|    }
  112|   255k|  }
  113|   127k|  node[u + 1].sum = node[u + 1].val + node[u + 1].sum_;
  114|   127k|}
  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|   127k|  for (int i = 1; i <= n; ++i) node[i] = {.val = rd.uw(), .k = -1};
                                        ^127k^127k
  ------------------
  |  Branch (126:19): [True: 100.00%, False: 0.00%]
  ------------------
  127|   127k|  for (int i = 1; i != n; ++i) {
                                        ^127k
  ------------------
  |  Branch (127:19): [True: 100.00%, False: 0.00%]
  ------------------
  128|   127k|    int u = rd.uh();
  129|   127k|    int v = rd.uh();
  130|   127k|    edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
  131|   127k|    edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
  132|   127k|  }
  133|      1|  build(0, 0);
  134|   148k|  while (q--) {
  ------------------
  |  Branch (134:10): [True: 100.00%, False: 0.00%]
  ------------------
  135|   148k|    let t = rd.u1();
  136|   148k|    if (t == 0) {
  ------------------
  |  Branch (136:9): [True: 33.55%, False: 66.45%]
  ------------------
  137|  49.9k|      int u = rd.uh() + 1;
  138|  49.9k|      int v = rd.uh() + 1;
  139|  49.9k|      access(u);
  140|  49.9k|      reverse(u);
  141|  49.9k|      access(v);
  142|  49.9k|      node[node[v].ch[0]].p = 0;
  143|  49.9k|      node[node[v].ch[0]].k = -1;
  144|  49.9k|      node[v].ch[0] = 0;
  145|  49.9k|      int w = rd.uh() + 1;
  146|  49.9k|      int x = rd.uh() + 1;
  147|  49.9k|      access(w);
  148|  49.9k|      reverse(w);
  149|  49.9k|      access(x);
  150|  49.9k|      node[w].p = x;
  151|  49.9k|      node[x].sum_ += node[w].sum;
  152|  49.9k|    }
  153|   148k|    if (t == 1) {
  ------------------
  |  Branch (153:9): [True: 33.26%, False: 66.74%]
  ------------------
  154|  49.4k|      int u = rd.uh() + 1;
  155|  49.4k|      u32 x = rd.uw();
  156|  49.4k|      access(u);
  157|  49.4k|      node[u].val += x;
  158|  49.4k|    }
  159|   148k|    if (t == 2) {
  ------------------
  |  Branch (159:9): [True: 33.19%, False: 66.81%]
  ------------------
  160|  49.3k|      int u = rd.uh() + 1;
  161|  49.3k|      int p = rd.uh() + 1;
  162|  49.3k|      access(u);
  163|  49.3k|      reverse(u);
  164|  49.3k|      access(p);
  165|  49.3k|      wt.ud(node[u].sum);
  166|  49.3k|    }
  167|   148k|  }
  168|      1|  return 0;
  169|      1|}