/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|   450k|def reverse(int x) -> void { node[x].rev ^= 1; }
   21|       |
   22|  1.02M|def pushdown(int x) -> void {
   23|  1.02M|  if (node[x].rev) {
  ------------------
  |  Branch (23:7): [True: 20.30%, False: 79.70%]
  ------------------
   24|   207k|    std::swap(node[x].ch[0], node[x].ch[1]);
   25|   207k|    node[node[x].ch[0]].k = 0;
   26|   207k|    node[node[x].ch[1]].k = 1;
   27|   207k|    reverse(node[x].ch[0]);
   28|   207k|    reverse(node[x].ch[1]);
   29|   207k|    node[x].rev = 0;
   30|   207k|  }
   31|  1.02M|}
   32|       |
   33|  1.01M|def maintain(int x) -> void {
   34|  1.01M|  node[x].size =
   35|  1.01M|      node[node[x].ch[0]].size + 1 + node[node[x].ch[1]].size + node[x].size_;
   36|  1.01M|  node[x].sum = node[node[x].ch[0]].sum + node[node[x].ch[1]].sum +
   37|  1.01M|                node[x].sum_ + node[x].size * node[x].add;
   38|  1.01M|}
   39|       |
   40|   686k|def rotateup(int x) -> void {
   41|   686k|  int p = node[x].p;
   42|   686k|  int g = node[p].p;
   43|   686k|  int k = node[x].k;
   44|   686k|  int t = node[p].k;
   45|   686k|  i64 b = node[x].add;
   46|   686k|  i64 a = node[p].add;
   47|   686k|  node[x].add = a + b;
   48|   686k|  node[p].add = -b;
   49|   686k|  node[node[x].ch[k ^ 1]].add += b;
   50|   686k|  node[node[x].ch[k ^ 1]].sum += b * node[node[x].ch[k ^ 1]].size;
   51|   686k|  node[node[x].ch[k ^ 1]].p = p;
   52|   686k|  node[node[x].ch[k ^ 1]].k = k;
   53|   686k|  node[p].ch[k] = node[x].ch[k ^ 1];
   54|   686k|  node[p].p = x;
   55|   686k|  node[p].k = k ^ 1;
   56|   686k|  node[x].ch[k ^ 1] = p;
   57|   686k|  node[x].p = g;
   58|   686k|  node[x].k = t;
   59|   686k|  if (t != -1) {
  ------------------
  |  Branch (59:7): [True: 32.77%, False: 67.23%]
  ------------------
   60|   224k|    node[g].ch[t] = x;
   61|   224k|  }
   62|   686k|  maintain(p);
   63|   686k|}
   64|       |
   65|   862k|def is_root(int x) -> bool { return node[x].k == -1; }
   66|       |
   67|   325k|def splay(int x) -> void {
   68|   325k|  pushdown(x);
   69|   593k|  while (!is_root(x)) {
  ------------------
  |  Branch (69:10): [True: 45.23%, False: 54.77%]
  ------------------
   70|   268k|    if (int p = node[x].p; is_root(p)) {
  ------------------
  |  Branch (70:28): [True: 39.89%, False: 60.11%]
  ------------------
   71|   107k|      pushdown(p);
   72|   107k|      pushdown(x);
   73|   107k|      rotateup(x);
   74|   161k|    } else {
   75|   161k|      int g = node[p].p;
   76|   161k|      pushdown(g);
   77|   161k|      pushdown(p);
   78|   161k|      pushdown(x);
   79|   161k|      if (node[x].k == node[p].k) {
  ------------------
  |  Branch (79:11): [True: 58.98%, False: 41.02%]
  ------------------
   80|  95.2k|        rotateup(p);
   81|  95.2k|        rotateup(x);
   82|  95.2k|      } else {
   83|  66.2k|        rotateup(x);
   84|  66.2k|        rotateup(x);
   85|  66.2k|      }
   86|   161k|    }
   87|   268k|  }
   88|   325k|}
   89|       |
   90|  69.0k|def access(int x) -> void {
   91|  69.0k|  splay(x);
   92|  69.0k|  node[node[x].ch[1]].k = -1;
   93|  69.0k|  node[x].sum_ += node[node[x].ch[1]].sum;
   94|  69.0k|  node[x].size_ += node[node[x].ch[1]].size;
   95|  69.0k|  node[x].ch[1] = 0;
   96|  69.0k|  maintain(x);
   97|   325k|  while (int p = node[x].p) {
  ------------------
  |  Branch (97:14): [True: 78.77%, False: 21.23%]
  ------------------
   98|   256k|    splay(p);
   99|   256k|    node[node[p].ch[1]].k = -1;
  100|   256k|    node[p].sum_ += node[node[p].ch[1]].sum;
  101|   256k|    node[p].size_ += node[node[p].ch[1]].size;
  102|   256k|    node[p].sum_ -= node[x].sum;
  103|   256k|    node[p].size_ -= node[x].size;
  104|   256k|    node[p].ch[1] = x;
  105|   256k|    node[x].k = 1;
  106|   256k|    rotateup(x);
  107|   256k|    maintain(x);
  108|   256k|  }
  109|  69.0k|}
  110|       |
  111|       |int head[N];
  112|       |struct {
  113|       |  int to;
  114|       |  int next;
  115|       |} edge[N * 2];
  116|       |
  117|   167k|def build(int u, int p) -> void {
  118|   501k|  for (int e = head[u]; e; e = edge[e].next) {
                                         ^334k
  ------------------
  |  Branch (118:25): [True: 66.67%, False: 33.33%]
  ------------------
  119|   334k|    int v = edge[e].to;
  120|   334k|    if (v != p) {
  ------------------
  |  Branch (120:9): [True: 50.00%, False: 50.00%]
  ------------------
  121|   167k|      build(v, u);
  122|   167k|      node[v + 1].p = u + 1;
  123|   167k|      node[u + 1].sum_ += node[v + 1].sum;
  124|   167k|      node[u + 1].size_ += node[v + 1].size;
  125|   167k|    }
  126|   334k|  }
  127|   167k|  node[u + 1].sum = node[u + 1].sum_;
  128|   167k|  node[u + 1].size = node[u + 1].size_ + 1;
  129|   167k|}
  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|   167k|  for (int i = 1; i <= n; ++i) node[i] = {.sum_ = rd.uw(), .k = -1};
                                        ^167k^167k
  ------------------
  |  Branch (141:19): [True: 100.00%, False: 0.00%]
  ------------------
  142|   167k|  for (int i = 1; i != n; ++i) {
                                        ^167k
  ------------------
  |  Branch (142:19): [True: 100.00%, False: 0.00%]
  ------------------
  143|   167k|    int u = rd.uh();
  144|   167k|    int v = rd.uh();
  145|   167k|    edge[i * 2 | 0] = {v, head[u]}, head[u] = i * 2 | 0;
  146|   167k|    edge[i * 2 | 1] = {u, head[v]}, head[v] = i * 2 | 1;
  147|   167k|  }
  148|      1|  build(0, 0);
  149|  25.8k|  while (q--) {
  ------------------
  |  Branch (149:10): [True: 100.00%, False: 0.00%]
  ------------------
  150|  25.8k|    let t = rd.u1();
  151|  25.8k|    if (t == 0) {
  ------------------
  |  Branch (151:9): [True: 33.64%, False: 66.36%]
  ------------------
  152|  8.69k|      int u = rd.uh() + 1;
  153|  8.69k|      int v = rd.uh() + 1;
  154|  8.69k|      access(u);
  155|  8.69k|      reverse(u);
  156|  8.69k|      access(v);
  157|  8.69k|      node[node[v].ch[0]].p = 0;
  158|  8.69k|      node[node[v].ch[0]].k = -1;
  159|  8.69k|      node[node[v].ch[0]].add += node[v].add;
  160|  8.69k|      node[v].ch[0] = 0;
  161|  8.69k|      int w = rd.uh() + 1;
  162|  8.69k|      int x = rd.uh() + 1;
  163|  8.69k|      access(w);
  164|  8.69k|      reverse(w);
  165|  8.69k|      access(x);
  166|  8.69k|      node[w].p = x;
  167|  8.69k|      node[w].add -= node[x].add;
  168|  8.69k|      node[w].sum -= node[x].add * node[w].size;
  169|  8.69k|      node[x].sum_ += node[w].sum;
  170|  8.69k|      node[x].size_ += node[w].size;
  171|  8.69k|    }
  172|  25.8k|    if (t == 1) {
  ------------------
  |  Branch (172:9): [True: 32.93%, False: 67.07%]
  ------------------
  173|  8.50k|      int u = rd.uh() + 1;
  174|  8.50k|      int p = rd.uh() + 1;
  175|  8.50k|      i64 x = rd.uw();
  176|  8.50k|      access(u);
  177|  8.50k|      reverse(u);
  178|  8.50k|      access(p);
  179|  8.50k|      node[u].add += x;
  180|  8.50k|      node[u].sum += x * node[u].size;
  181|  8.50k|    }
  182|  25.8k|    if (t == 2) {
  ------------------
  |  Branch (182:9): [True: 33.43%, False: 66.57%]
  ------------------
  183|  8.63k|      int u = rd.uh() + 1;
  184|  8.63k|      int p = rd.uh() + 1;
  185|  8.63k|      access(u);
  186|  8.63k|      reverse(u);
  187|  8.63k|      access(p);
  188|  8.63k|      i64 ans = node[u].sum + node[u].size * node[p].add;
  189|  8.63k|      wt.ud(ans);
  190|  8.63k|    }
  191|  25.8k|  }
  192|      1|  return 0;
  193|      1|}