#P10599. BZOJ2164 采矿
BZOJ2164 采矿
Background
This problem comes from the original BZOJ. We acknowledge that the copyright of the statement and the original testdata belongs to the original BZOJ or the problem author who authorized BZOJ to use it. If you are the copyright holder and believe that we have infringed your rights, please contact us.
Problem Description
A huge army of cg has discovered a city with extremely rich mineral resources, and they plan to carry out a new mining strategy in this city.
The city can be viewed as a rooted tree with nodes. We label each node with an integer from to . For convenience, for any non-root node , the label of any ancestor of is strictly smaller than . Each node on the tree represents a mining site, and each edge represents a road.
As a squad leader in the cg army, you have subordinates. You have a 2D dynamic information table, where denotes the value in row and column . When you are allowed to mine a certain region, you can assign your subordinates to mining sites. If you assign people to mining site , you can obtain units of minerals.
A minable region is described as follows: you are given a pair of mining sites , and it is guaranteed that is an ancestor of (here “ancestor” includes itself). The subtree rooted at is the controlled area, where you may freely assign subordinates to any nodes in this subtree. The simple path from to (excluding but including ; if , then it includes ) is the exploration path, and on this path you may choose at most one mining site to assign subordinates to. The profit of this mining operation is the sum of the profits of all mining sites to which at least one subordinate is assigned.
Input Format
The first line contains positive integers . Here is the number of mining sites, and is the number of subordinates. are values related to the dynamic information table.
The second line contains positive integers. The -th integer is , which indicates the parent of node .
Next, you need to generate groups of data in order using the method described below. Each group contains numbers. For the -th group, these numbers are the values in row of the information table. Immediately after that, one line contains a positive integer , indicating the number of events.
Finally, lines follow, each describing an event. Each event starts with an integer or .
- If it is , then a positive integer follows, meaning the information table is updated. You need to generate a group of numbers to replace the values in row of the table.
- If it is , then two positive integers follow, meaning a minable region appears, and you need to answer the profit of this mining operation.
All numbers on the same line are separated by exactly one space, with no extra spaces or newlines.
The data generation method is as follows: each time, generate a group of numbers in nondecreasing order to replace one row of the dynamic information table. The -th smallest number replaces the value in column of that row. Call the following code times and sort the results to obtain one group of data. (Note that duplicate numbers may appear.)
Function GetInt
A←((A xor B)+(B div X)+(B * X))and Y
B←((A xor B)+(A div X)+(A * X))and Y
return (A xor B)mod Q
Here are stored as 32-bit signed integers (C/C++ type signed long int, Pascal type longint). , . xor is bitwise XOR, div is integer division, and is bitwise AND, and mod is modulo. Since only the lowest bits are kept, it is easy to see that we do not need to worry about overflow. Note that each call will modify both and .
Output Format
For each mining event (an event starting with ), output one line with one integer, the profit for that operation.
10 5 1 2 10
1 1 3 3 4 4 6 6 9
4
1 6 3
1 9 1
0 1
1 1 1
11
9
12
Hint
Sample Explanation
The initial information table is:
| 0 | 1 | 2 | ||
|---|---|---|---|---|
| 0 | 5 | 7 | 9 | |
| 1 | 2 | 3 | 4 | 5 |
| 0 | 1 | 2 | ||
| 2 | 4 | 7 | 8 | 8 |
| 0 | 2 | 3 | 9 | |
| 1 | 3 | 5 | 6 | 8 |
| 3 | 3 | 7 | ||
| 0 | 1 | 2 | 3 | 9 |
| 0 | 1 | 4 | ||
After the change, row becomes:
1 1 1 4 7
In the first mining operation, you may assign subordinates freely among mining sites , and you may choose one of mining sites or to assign subordinates. One optimal assignment is to assign people to mining site and person to mining site . In the second mining operation, you may assign at mining site , and you may choose one among mining sites to assign. One optimal assignment is to assign person to mining site and people to mining site .
Constraints
There is of the testdata where, for every integer satisfying , . In this part, of the testdata (i.e. of all testdata) satisfies , , .
Besides the above, there is another of the testdata satisfying , , .
For of the testdata, , , . For every integer satisfying , . , .
Translated by ChatGPT 5