#P10107. [GDKOI2023 提高组] 树
[GDKOI2023 提高组] 树
Problem Description
Given a rooted tree with nodes. The nodes are numbered starting from . The root is node . Each node has a positive integer weight .
There are queries. For one query, given , let all nodes in the subtree of node (including itself) whose distance to node is at most be . Then the answer to this query is:
$$(v_{c_1} ⊕ d(c_1, x)) + (v_{c_2} ⊕ d(c_2, x)) + \cdots + (v_{c_m} ⊕ d(c_m, x))$$Here, denotes the number of edges on the unique simple path between nodes and in the tree, and . denotes the XOR operation.
Input Format
The first line contains an integer , representing the size of the tree.
The second line contains integers, representing .
The third line contains integers, in order representing nodes to . Each integer is the parent index of the corresponding node.
The fourth line contains an integer .
The next lines each contain two integers , representing a query .
Output Format
Output lines in total. The -th line contains one integer, representing the answer to the -th query.
10
9 3 0 7 4 8 8 7 2 5
1 1 2 2 3 6 6 8 7
10
8 2
2 1
5 1
4 1
4 1
1 4
4 1
6 3
4 1
1 4
10
14
4
7
7
55
7
30
7
55
Hint
For of the testdata, .
For of the testdata, .
For another of the testdata, .
For another of the testdata, .
For another of the testdata, .
For another of the testdata, .
For of the testdata, , , , .
Translated by ChatGPT 5