#P3833. [SHOI2012] 魔法树
[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 nodes. Node is the root, and the parent of each node is denoted by , with the guarantee that . 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, 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 to the number of fruits on every node on the path between nodes and .
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 .
Input Format
The first line contains a positive integer , the total number of nodes in the fruit tree. Nodes are labeled , and is the root.
The next lines each contain two integers , indicating that is the parent of .
Then follows a positive integer , the number of operations.
Then there are lines, each being one of the following two types:
A u v d: Add to the number of fruits on every node along the path from to . It is guaranteed that , .Q u: Query the total number of fruits in the subtree rooted at , including itself.
Output Format
For every Q operation, output the answer in order, one per line. The answer may exceed , but will not exceed .
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
京公网安备 11011102002149号