#P3833. [SHOI2012] 魔法树

    ID: 2778 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2012线段树倍增各省省选上海最近公共祖先,LCA树链剖分,树剖

[SHOI2012] 魔法树

Description

Harry Potter has learned a new kind of magic: he can change the number of fruits on a tree. Overjoyed, he found a huge fruit tree to test his new spell.

This fruit tree has NN nodes. Node 00 is the root, and the parent of each node uu is denoted by fa[u]fa[u], with the guarantee that fa[u]<ufa[u] < u. Initially, all the fruits on this tree were removed by Dumbledore’s magic, so every node on this fruit tree has no fruits (that is, 00 fruits).

Unfortunately, Harry’s magic is not yet perfect; he can only uniformly increase the number of fruits on all nodes along a path in the tree by a fixed amount. In other words, Harry’s magic can be described as: A u v d. This means adding dd to the number of fruits on every node on the path between nodes uu and vv.

Next, to help check whether Harry’s magic is working, you need to tell him some information about the fruit tree during the process: Q u. This asks for the total number of fruits currently in the subtree rooted at node uu.

Input Format

The first line contains a positive integer NN (1N100000)(1 \leq N \leq 100000), the total number of nodes in the fruit tree. Nodes are labeled 0,1,,N10, 1, \dots, N - 1, and 00 is the root.

The next N1N - 1 lines each contain two integers a,ba, b (0a<b<N)(0 \leq a < b < N), indicating that aa is the parent of bb.

Then follows a positive integer QQ (1Q100000)(1 \leq Q \leq 100000), the number of operations.

Then there are QQ lines, each being one of the following two types:

  1. A u v d: Add dd to the number of fruits on every node along the path from uu to vv. It is guaranteed that 0u,v<N0 \leq u, v < N, 0<d<1000000 < d < 100000.
  2. Q u: Query the total number of fruits in the subtree rooted at uu, including uu itself.

Output Format

For every Q operation, output the answer in order, one per line. The answer may exceed 2322^{32}, but will not exceed 101510^{15}.

4
0 1
1 2
2 3
4
A 1 3 1
Q 0
Q 1
Q 2
3
3
2

Hint

Translated by ChatGPT 5