#ds1004. 动态树
动态树
Dynamic Tree
Problem Description
Do not forget that this is a dynamic tree — it is dynamic at every moment. Xiao Ming asks you to maintain two kinds of events on this tree.
0 u delta: the tree grows some fruits, i.e. every node in the subtree rooted at grows fruits.1 k u1 v1 ... uk vk: Xiao Ming wants you to compute the number of fruits on several branches. A branch is a segment of a path from some node to the root. Each time Xiao Ming selects several branches and asks for the sum of the fruits on the nodes belonging to these branches. Note that branches may overlap; in that case the fruits on the overlapping part of the nodes are counted only once.
Input Format
The first line contains one integer (), the number of nodes.
The next lines each contain two numbers , indicating a direct edge between fruit and fruit . Nodes are numbered starting from .
Then one integer () follows, the number of events.
The last lines each begin with either or .
- If it is , this is event . The next two integers on this line are (), meaning every node in the subtree rooted at grows fruits.
- If it is , this is event . The next integer on this line is (), the number of branches involved in this query, followed by pairs of integers ; each branch goes from node to node .
Since the number of fruits may be very large, output the result modulo .
Output Format
For each event , output the queried number of fruits.
Sample Input
5
1 2
2 3
2 4
1 5
3
0 1 1
0 2 3
1 2 3 1 1 4
Sample Output
13