#P1372. [CSP-S 2019] 划分

[CSP-S 2019] 划分

[CSP-S 2019] 划分

  • 时间限制:2 秒
  • 内存限制:1024 MiB

题目描述

给定 nn 个正整数 a1,a2,…,ana_1,a_2,\ldots,a_n。你需要把它们划分成若干个非空连续段,并要求各段元素和从左到右单调不降。

若各段元素和依次为 s1,s2,…,sks_1,s_2,\ldots,s_k,则划分必须满足

s1≤s2≤⋯≤sk.s_1\le s_2\le\cdots\le s_k.

该划分的代价为

s12+s22+⋯+sk2.s_1^2+s_2^2+\cdots+s_k^2.

请计算所有合法划分中的最小代价。也可以只划分成一个包含全部元素的段。

输入格式

由于数据规模较大,部分测试点中的 aia_i 由输入参数生成。

第一行包含两个整数 n,typen,type。

当 type=0type=0 时

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n。

当 type=1type=1 时

第二行包含六个整数 x,y,z,b1,b2,mx,y,z,b_1,b_2,m。

接下来 mm 行,第 jj 行包含三个正整数 pj,lj,rjp_j,l_j,r_j。

保证 n≥2n\ge2。当 n>2n>2 时,对 3≤i≤n3\le i\le n 定义

bi=(xbi−1+ybi−2+z) mod 230.b_i=(x b_{i-1}+y b_{i-2}+z)\bmod 2^{30}.

令 p0=0p_0=0,并保证

0=p0<p1<p2<⋯<pm=n.0=p_0<p_1<p_2<\cdots<p_m=n.

若 pj−1<i≤pjp_{j-1}<i\le p_j,则

ai=(bi mod (rj−lj+1))+lj.a_i=\bigl(b_i\bmod(r_j-l_j+1)\bigr)+l_j.

这种生成方式只用于减小输入文件大小,算法不应依赖生成器的特殊性质。

输出格式

输出一行一个整数,表示最小代价。答案可能超过 64 位有符号整数范围。

样例输入 1

5 0
5 1 7 9 9

样例输出 1

247

样例说明 1

最优划分为 {5,1},{7},{9},{9}\{5,1\},\{7\},\{9\},\{9\},段和为 6,7,9,96,7,9,9,代价为

62+72+92+92=247.6^2+7^2+9^2+9^2=247.

样例输入 2

10 0
5 6 7 7 4 6 2 13 19 9

样例输出 2

1256

样例输入 3

10000000 1
123 456 789 12345 6789 3
2000000 123456789 987654321
7000000 234567891 876543219
10000000 456789123 567891234

样例输出 3

4972194419293431240859891640

数据范围

对于所有测试数据,保证:

  • 所有测试点满足 type∈{0,1}type\in\{0,1\},1≤n≤4×1071\le n\le4\times10^7,1≤ai≤1091\le a_i\le10^9,1≤m≤1051\le m\le10^5,1≤li≤ri≤1091\le l_i\le r_i\le10^9,0≤x,y,z,b1,b2<2300\le x,y,z,b_1,b_2<2^{30}。其中 type=1type=1 时保证 n≥2n\ge2。

对于 type=0type=0 的测试点,答案不超过 4×10184\times10^{18}。

共有 2525 个独立测试点,每个测试点 44 分,得分累加。

测试点编号 分值 特殊限制
1∼61\sim6 44 type=0type=0,n≤50n\le50
7∼97\sim9 type=0type=0,n≤400n\le400
10∼1610\sim16 type=0type=0,n≤5000n\le5000
17∼1917\sim19 type=0type=0,n≤5×105n\le5\times10^5
20∼2520\sim25 无
测试点编号 n≤n\le ai≤a_i\le typetype
1∼31\sim3 1010 00
4∼64\sim6 5050 10310^3
7∼97\sim9 400400 10410^4
10∼1610\sim16 50005000 10510^5
17∼2217\sim22 5×1055\times10^5 10610^6
23∼2523\sim25 4×1074\times10^7 10910^9 11