#P10599. BZOJ2164 采矿

    ID: 12081 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP线段树背包 DP树链剖分

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 nn nodes. We label each node with an integer from 11 to nn. For convenience, for any non-root node vv, the label of any ancestor of vv is strictly smaller than vv. 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 mm subordinates. You have a 2D dynamic information table, where Ti,jT_{i,j} denotes the value in row ii and column jj. When you are allowed to mine a certain region, you can assign your subordinates to mining sites. If you assign jj people to mining site ii, you can obtain Ti,jT_{i,j} units of minerals.

A minable region is described as follows: you are given a pair of mining sites (u,v)(u,v), and it is guaranteed that vv is an ancestor of uu (here “ancestor” includes uu itself). The subtree rooted at uu is the controlled area, where you may freely assign subordinates to any nodes in this subtree. The simple path from uu to vv (excluding uu but including vv; if u=vu=v, then it includes uu) 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 55 positive integers n,m,A,B,Qn,m,A,B,Q. Here nn is the number of mining sites, and mm is the number of subordinates. A,B,QA,B,Q are values related to the dynamic information table.

The second line contains n1n-1 positive integers. The ii-th integer is Fi+1F_{i+1}, which indicates the parent of node i+1i+1.

Next, you need to generate nn groups of data in order using the method described below. Each group contains mm numbers. For the ii-th group, these mm numbers are the mm values in row ii of the information table. Immediately after that, one line contains a positive integer CC, indicating the number of events.

Finally, CC lines follow, each describing an event. Each event starts with an integer 00 or 11.

  • If it is 00, then a positive integer pp follows, meaning the information table is updated. You need to generate a group of mm numbers to replace the mm values in row pp of the table.
  • If it is 11, then two positive integers u,vu,v 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 mm numbers in nondecreasing order to replace one row of the dynamic information table. The jj-th smallest number replaces the value in column jj of that row. Call the following code mm 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 A,B,QA,B,Q are stored as 32-bit signed integers (C/C++ type signed long int, Pascal type longint). X=216X=2^{16}, Y=2311Y=2^{31}-1. xor is bitwise XOR, div is integer division, and is bitwise AND, and mod is modulo. Since only the lowest 3131 bits are kept, it is easy to see that we do not need to worry about overflow. Note that each call will modify both AA and BB.

Output Format

For each mining event (an event starting with 11), 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 11 becomes:

1 1 1 4 7

In the first mining operation, you may assign subordinates freely among mining sites 6,8,9,106,8,9,10, and you may choose one of mining sites 33 or 44 to assign subordinates. One optimal assignment is to assign 44 people to mining site 66 and 11 person to mining site 88. In the second mining operation, you may assign at mining site 99, and you may choose one among mining sites 6,4,3,16,4,3,1 to assign. One optimal assignment is to assign 11 person to mining site 99 and 44 people to mining site 66.

Constraints

There is 50%50\% of the testdata where, for every integer ii satisfying 2in2\leq i\leq n, Fi=i1F_i=i-1. In this part, 40%40\% of the testdata (i.e. 20%20\% of all testdata) satisfies n500n\leq 500, m20m\leq 20, C500C\leq 500.

Besides the above, there is another 40%40\% of the testdata satisfying n500n\leq 500, m20m\leq 20, C500C\leq 500.

For 100%100\% of the testdata, 1n200001\leq n\leq 20000, 1m501\leq m\leq 50, 1C20001\leq C\leq 2000. For every integer ii satisfying 2in2\leq i\leq n, 1Fi<i1\leq F_i<i. 1A,B23111\leq A,B\leq 2^{31}-1, 1Q100001\leq Q\leq 10000.

Translated by ChatGPT 5