CF2241G.Summmon

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Note that the answer for this problem might not fit in int64 or long long. It is recommended to use int128.

For any array bb of length mm, define f(b)f(b) as the minimum possible value of max⁡(b)−min⁡(b)\max(b) - \min(b) that can be achieved by performing the following operation any number of times:

  • Choose any index 1≤i<m1 \le i \lt m, and do exactly one of the following:
    1. Set bi+1:=bi+1+bib_{i+1} := b_{i+1} + b_i,
    2. Set bi+1:=bi+1−bib_{i+1} := b_{i+1} - b_i.

You are given an array aa of length nn. Your task is to compute the sum of ff over all the subarrays∗^{\text{∗}} of aa. More formally, determine the value of

\\sum\_{1 \\le l \\le r \\le n} f(\[a\_l,a\_{l+1},\\dots,a\_r\]).

∗^{\text{∗}}An array bb is a subarray of an array aa if bb can be obtained from aa by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.

注意:本题的答案可能无法用 int64 或 long long 类型表示。建议使用 int128。

对于任意长度为 mm 的数组 bb,定义 f(b)f(b) 为:通过对 bb 执行以下操作任意多次后,所能达到的 max⁡(b)−min⁡(b)\max(b) - \min(b) 的最小可能值:

  • 任选一个下标 1≤i<m1 \le i \lt m,并恰好执行以下操作之一:
    1. 将 bi+1b_{i+1} 更新为 bi+1+bib_{i+1} + b_i,
    2. 将 bi+1b_{i+1} 更新为 bi+1−bib_{i+1} - b_i。

给定一个长度为 nn 的数组 aa。你的任务是计算 ff 在 aa 的所有子数组∗^{\text{∗}} 上的取值之和。更准确地说,需计算如下表达式的值:

∑1≤l≤r≤nf([al,al+1,…,ar]).\sum_{1 \le l \le r \le n} f([a_l,a_{l+1},\dots,a_r]).

∗^{\text{∗}} 若数组 bb 可通过从数组 aa 的开头删除若干(可能为零或全部)元素、再从结尾删除若干(可能为零或全部)元素而得到,则称 bb 是 aa 的一个子数组。特别地,一个数组是其自身的子数组。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. Description of each test case follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2\cdot10^5) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the elements of the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。接下来是每个测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2\cdot10^5)—— 数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 数组 aa 的元素。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot10^5。

输出格式

For each test case, print a single integer — the value of ∑1≤l≤r≤nf([al,al+1,…,ar])\sum_{1 \le l \le r \le n} f([a_l,a_{l+1},\dots,a_r]).

对于每个测试用例,输出一个整数——即 ∑1≤l≤r≤nf([al,al+1,…,ar])\sum_{1 \le l \le r \le n} f([a_l,a_{l+1},\dots,a_r]) 的值。

输入输出样例

  • 输入#1

    5
    3
    6 4 8
    4
    1 2 3 4
    9
    9 9 8 2 4 4 3 5 3
    6
    18 12 24 9 6 36
    6
    36 24 18 12 9 6

    输出#1

    4
    3
    39
    72
    111

说明/提示

For the first test case, let us look at all subarrays.

  • For the single-element subarrays [6][6], [4][4], and [8][8], no operation can change anything, so each of them contributes 00.
  • For [6,4][6,4], the first element is fixed as 66. The second element can only be changed to 4+6=104+6=10 or 4−6=−24-6=-2, so the gap would become 44 or 88. Hence, doing no operation is best, and the contribution is 22.
  • For [4,8][4,8], we choose i=1i=1 and apply b2:=b2−b1b_{2}:=b_{2}-b_{1}. Then the subarray becomes [4,4][4,4], so its contribution is 00.
  • For [6,4,8][6,4,8], we keep the first two elements as they are and choose i=2i=2. Then we apply b3:=b3−b2b_{3}:=b_{3}-b_{2}, so 88 becomes 44. The array becomes [6,4,4][6,4,4], and therefore max⁡(b)−min⁡(b)=6−4=2\max(b)-\min(b)=6-4=2. It can be shown that this is the optimal value.

Therefore, the answer for the first test case is 0+0+0+2+0+2=40+0+0+2+0+2=4.

For the second test case, every subarray of length 11 contributes 00. Among the remaining subarrays, only [2,3][2,3], [3,4][3,4], and [2,3,4][2,3,4] have a non-zero contribution, each contributing 11. Therefore, the answer is 1+1+1=31+1+1=3.

对于第一个测试用例,我们考察所有子数组:

  • 对于单元素子数组 [6][6]、[4][4] 和 [8][8],无法执行任何操作,因此每个子数组的贡献均为 00。
  • 对于 [6,4][6,4],首元素固定为 66;第二个元素只能被修改为 4+6=104+6=10 或 4−6=−24-6=-2,此时差值(即 max⁡−min⁡\max - \min)将变为 44 或 88。因此,不执行任何操作是最优策略,其贡献为 22。
  • 对于 [4,8][4,8],我们选取 i=1i=1 并执行操作 b2:=b2−b1b_{2}:=b_{2}-b_{1},子数组变为 [4,4][4,4],其贡献为 00。
  • 对于 [6,4,8][6,4,8],我们保持前两个元素不变,并选取 i=2i=2,执行操作 b3:=b3−b2b_{3}:=b_{3}-b_{2},使 88 变为 44。数组变为 [6,4,4][6,4,4],因此 max⁡(b)−min⁡(b)=6−4=2\max(b)-\min(b)=6-4=2。可以证明该值即为最优值。

因此,第一个测试用例的答案为 0+0+0+2+0+2=40+0+0+2+0+2=4。

对于第二个测试用例,所有长度为 11 的子数组均贡献 00。在其余子数组中,仅有 [2,3][2,3]、[3,4][3,4] 和 [2,3,4][2,3,4] 具有非零贡献,且每个子数组的贡献均为 11。因此,答案为 1+1+1=31+1+1=3。

输入解题思路,AI测评打分。不知道怎么写?

首页