Description
You are given a rooted tree with vertices, identified by indices . The root has index . For each , the vertex (i.e., the vertex with index ) has an integer assigned to it. Let be the value of the bitwise (henceforth denoted by ) of the values on the simple path from the vertex to the root. (Note that the simple path from a vertex to a vertex includes both and .) Let the power of the tree be the value of
and let the superpower of the tree be the value of (note the difference in ranges)
For a clarifying example, see the explanation of the sample test cases below.
We will say that a vertex belongs to the subtree of vertex if belongs to the simple path from the vertex to the root. Note that the subtree of a vertex includes the vertex itself.
You are presented with updates. Each update is described by two integers, and , and it requires you to set for each vertex in the subtree of vertex . After each update, you should output the power and superpower of the current tree.
As output values can be large, print them modulo .
Input format
The first line of the input contains the integers and .
The second line of the input contains integers, namely , which determine the structure of the tree. For each , is the index of the parent of vertex , and it holds that .
The third line of the input contains integers, namely . These are the values assigned to the vertices.
Each of the following lines contains two integers, () and . These integers specify the individual updates.
Output format
Output lines. Each line should contain two integers separated by a space. In the first line, print the power and the superpower (modulo ) of the initial tree. In the -th line of the remaining lines (), print the power and the superpower (modulo ) of the tree after the -th update.
Input bounds
- .
- for each .
- for each update .
Scoring
For a given test case, your solution will receive of the score if it correctly computes all power values but incorrectly computes at least one superpower value for that test case.
Likewise, of the score for a given test case will be awarded to a solution that correctly computes all superpower values for that test case but incorrectly computes at least one power value.
Subtasks
- (4 points) .
- (7 points) .
- (13 points) .
- (6 points) (for each ), and (for each and for each update ).
- (7 points) (for each ).
- (12 points) (for each and for each update ).
- (14 points) .
- (11 points) .
- (26 points) No additional constraints.
3 3
0 0
7 3 4
1 6
2 2
0 3
196 61
169 50
81 14
25 6
Initially, we have
Therefore, the power of the tree is equal to
$$\begin{gathered} f_{0} \cdot f_{0}+f_{0} \cdot f_{1}+f_{0} \cdot f_{2}+f_{1} \cdot f_{0}+f_{1} \cdot f_{1}+f_{1} \cdot f_{2}+f_{2} \cdot f_{0}+f_{2} \cdot f_{1}+f_{2} \cdot f_{2}= \\ =7 \cdot 7+7 \cdot 3+7 \cdot 4+3 \cdot 7+3 \cdot 3+3 \cdot 4+4 \cdot 7+4 \cdot 3+4 \cdot 4=196 . \end{gathered}$$The superpower is equal to
$$f_{0} \cdot f_{1}+f_{0} \cdot f_{2}+f_{1} \cdot f_{2}=7 \cdot 3+7 \cdot 4+3 \cdot 4=61 \text {. }$$After the first update:
$$\begin{gathered} a_{0}=7, a_{1}=3 \& 6=2, a_{2}=4 ; \\ f_{0}=7, f_{1}=2, f_{2}=4 . \end{gathered}$$After the second update:
$$\begin{gathered} a_{0}=7, a_{1}=2, a_{2}=4 \& 2=0 \\ f_{0}=7, f_{1}=2, f_{2}=0 \end{gathered}$$After the third update:
$$\begin{gathered} a_{0}=7 \& 3=3, a_{1}=2 \& 3=2, a_{2}=0 \& 3=0 \\ f_{0}=3, f_{1}=2, f_{2}=0 \end{gathered}$$4 2
0 0 1
6 5 6 2
1 2
0 3
256 84
144 36
16 4
Initially, we have
$$f_{0}=6, f_{1}=6 \& 5=4, f_{2}=6 \& 6=6, f_{3}=2 \& 5 \& 6=0$$After the first update:
$$\begin{gathered} a_{0}=6, a_{1}=5 \& 2=0, a_{2}=6, a_{3}=2 \& 2=2 ; \\ f_{0}=6, f_{1}=0, f_{2}=6, f_{3}=2 \& 0=0 \end{gathered}$$After the second update:
$$\begin{gathered} a_{0}=7, a_{1}=2, a_{2}=4 \& 2=0 \\ f_{0}=7, f_{1}=2, f_{2}=0 \end{gathered}$$7 3
0 0 1 1 2 2
7 6 5 7 3 4 2
4 4
3 3
2 1
900 367
784 311
576 223
256 83