8837: [yosupo] Tree - Dynamic Tree Vertex Add Subtree Sum
[Creator : ]
Description
You are given a tree with $N$ vertices. Its edges are $(u_i, v_i)$. Its $i$-th vertex has a value $a_i$ written on it.
Process $Q$ queries. It is guaranteed that the graph is a tree after each query.
- 0 $u$ $v$ $w$ $x$: Remove an edge $(u, v)$ and add an edge $(w, x)$.
- 1 $p$ $x$: $a_p \gets a_p + x$
- 2 $v$ $p$: For an edge $(v, p)$ where $p$ is considered the parent, output the sum of weights of all vertices in the subtree of $v$.
Process $Q$ queries. It is guaranteed that the graph is a tree after each query.
- 0 $u$ $v$ $w$ $x$: Remove an edge $(u, v)$ and add an edge $(w, x)$.
- 1 $p$ $x$: $a_p \gets a_p + x$
- 2 $v$ $p$: For an edge $(v, p)$ where $p$ is considered the parent, output the sum of weights of all vertices in the subtree of $v$.
Input
$N$ $Q$
$a_0$ $a_1$ ... $a_{N - 1}$
$u_0$ $v_0$
$u_1$ $v_1$
:
$u_{N - 2}$ $v_{N - 2}$
$\textrm{Query}_0$
$\textrm{Query}_1$
:
$\textrm{Query}_{Q - 1}$
$a_0$ $a_1$ ... $a_{N - 1}$
$u_0$ $v_0$
$u_1$ $v_1$
:
$u_{N - 2}$ $v_{N - 2}$
$\textrm{Query}_0$
$\textrm{Query}_1$
:
$\textrm{Query}_{Q - 1}$
Constraints
$1 \leq N, Q \leq 200,000$
$0 \leq a_i, x \leq 10^9$
$0 \leq p, u_i, v_i < N$
$(u_i, v_i)$ is a tree
$0 \leq a_i, x \leq 10^9$
$0 \leq p, u_i, v_i < N$
$(u_i, v_i)$ is a tree
Sample 1 Input
5 7
1 10 100 1000 10000
0 1
1 2
2 3
1 4
2 1 2
1 1 100000
2 1 2
0 1 2 2 0
2 0 2
0 2 3 3 1
2 1 4
Sample 1 Output
10011
110011
110011
101111