#P5665. [CSP-S 2019] 划分

    ID: 4621 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心2019单调队列CSP S 提高级

[CSP-S 2019] 划分

Description

In 2048, at the exam site of the 30th CSP certification, contestant Xiaoming opened the first problem. The sample of this problem has nn groups of testdata, numbered from 1n1 \sim n, and the size of the ii-th testdata is aia_i.

Xiaoming designed a brute-force program for this problem. For a piece of testdata of size uu, the running time of the program is u2u^2. However, after the program finishes running on a piece of testdata of size uu, it will produce wrong results on any piece of testdata whose size is less than uu. The aia_i in the sample are not necessarily increasing, but Xiaoming wants to run the sample correctly without modifying the program. So he decided to use a very primitive solution: divide all testdata into several segments, where the indices within each segment are continuous. Then, merge the testdata within the same segment into new testdata whose size equals the sum of sizes of the original testdata in that segment. Xiaoming will make the sizes of the new testdata non-decreasing.

That is, Xiaoming needs to find some split points 1k1<k2<<kp<n1 \leq k_1 \lt k_2 \lt \cdots \lt k_p \lt n such that

$$\sum_{i=1}^{k_1} a_i \leq \sum_{i=k_1+1}^{k_2} a_i \leq \cdots \leq \sum_{i=k_p+1}^{n} a_i$$

Note that pp can be 00, and in this case k0=0k_0 = 0, meaning Xiaoming can merge all testdata together and run it once.

Xiaoming hopes that, while running the sample correctly, the running time can be as small as possible, i.e., minimize:

$$\left(\sum_{i=1}^{k_1} a_i\right)^2 + \left(\sum_{i=k_1+1}^{k_2} a_i\right)^2 + \cdots + \left(\sum_{i=k_p+1}^{n} a_i\right)^2$$

Xiaoming finds this problem very interesting and asks you for help: given nn and aia_i, please compute the minimum running time of Xiaoming's program under the optimal partition.

Input Format

Because the Constraints of this problem are large, for some test points, aia_i will be generated inside the program.

The first line contains two integers n,typen, type. The meaning of nn is described above, and typetype indicates the input method.

  1. If type=0type = 0, then aia_i for this test point are given directly. The input then contains: on the second line, nn integers aia_i separated by spaces, representing the size of each group of testdata.
  2. If type=1type = 1, then aia_i for this test point will be generated specially; the generation method is given later. The input then contains: on the second line, six integers x,y,z,b1,b2,mx, y, z, b_1, b_2, m separated by spaces. In the next mm lines, the ii-th line (1im)(1 \leq i \leq m) contains three positive integers pi,li,rip_i, l_i, r_i separated by spaces.

For test points 232523 \sim 25 with type=1type = 1, aia_i are generated as follows:

Given integers x,y,z,b1,b2,mx, y, z, b_1, b_2, m, and mm triples (pi,li,ri)(p_i, l_i, r_i).

It is guaranteed that n2n \geq 2. If n>2n \gt 2, then for all 3in3 \leq i \leq n, $b_i = (x \times b_{i−1} + y \times b_{i−2} + z) \bmod 2^{30}$.

It is guaranteed that 1pin1 \leq p_i \leq n and pm=np_m = n. Let p0=0p_0 = 0, then pip_i also satisfies: for all 0i<m0 \leq i \lt m, pi<pi+1p_i \lt p_{i+1}.

For all 1jm1 \leq j \leq m, if the index i(1in)i (1 \leq i \leq n) satisfies pj1<ipjp_{j−1} \lt i \leq p_j, then

$$a_i = \left(b_i \bmod \left( r_j − l_j + 1 \right) \right) + l_j$$

The above data generation method is only to reduce the input size; the standard algorithm does not depend on this generation method.

Output Format

Output one integer in one line, representing the answer.

5 0
5 1 7 9 9
247
10 0
5 6 7 7 4 6 2 13 19 9
1256
10000000 1
123 456 789 12345 6789 3
2000000 123456789 987654321
7000000 234567891 876543219
10000000 456789123 567891234
4972194419293431240859891640

Hint

[Sample 1 Explanation]

The optimal partition is {5,1},{7},{9},{9}\{5,1\}, \{7\}, \{9\}, \{9\}. Since 5+17995 + 1 \leq 7 \leq 9 \leq 9, this partition is valid.

The answer is (5+1)2+72+92+92=247(5 + 1)^2 + 7^2 + 9^2 + 9^2 = 247.

Although the partition {5},{1},{7},{9},{9}\{5\}, \{1\}, \{7\}, \{9\}, \{9\} has a running time smaller than 247247, it is not valid because 5>15 \gt 1.

Although the partition {5},{1,7},{9},{9}\{5\}, \{1,7\}, \{9\}, \{9\} is valid, its running time is 251251, which is larger than 247247.

[Sample 2 Explanation]

The optimal partition is $\{5\}, \{6\}, \{7\}, \{7\}, \{4,6,2\}, \{13\}, \{19,9\}$.

[Constraints]

::cute-table[]{tuack} | Test Point ID | nn \leq | aia_i \leq | type=type = | | :--: | :-: | :-: | :-: | | 131 \sim 3 | 1010 | 1010 | 00 | | 464 \sim 6 | 5050 | 10310^3 | ^ | | 797 \sim 9 | 400400 | 10410^4 | ^ | | 101610 \sim 16 | 50005000 | 10510^5 | ^ | | 172217 \sim 22 | 5×1055 \times 10^5 | 10610^6 | ^ | | 232523 \sim 25 | 4×1074 \times 10^7 | 10910^9 | 11 |

For all test points with type=0type=0, it is guaranteed that the final output answer 4×1018\leq 4 \times 10^{18}.

All test points satisfy: type{0,1}type \in \{0,1\}, 2n4×1072 \leq n \leq 4 \times 10^7, 1ai1091 \leq a_i \leq 10^9, 1m1051 \leq m \leq 10^5, 1liri1091 \leq l_i \leq r_i \leq 10^9, 0x,y,z,b1,b2<2300 \leq x,y,z,b_1,b_2 \lt 2^{30}

Translated by ChatGPT 5