CF1827B2.Range Sorting (Hard Version)
提高+/省选-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The only difference between this problem and the easy version is the constraints on t and n.
You are given an array a, consisting of n distinct integers a1,a2,…,an.
Define the beauty of an array p1,p2,…pk as the minimum amount of time needed to sort this array using an arbitrary number of range-sort operations. In each range-sort operation, you will do the following:
- Choose two integers l and r (1≤l<r≤k).
- Sort the subarray pl,pl+1,…,pr in r−l seconds.
Please calculate the sum of beauty over all subarrays of array a.
A subarray of an array is defined as a sequence of consecutive elements of the array.
此题与简单版本的唯一区别在于 t 和 n 的约束条件。
给你一个由 n 个互不相同的整数 a1,a2,…,an 构成的数组 a。
定义数组 p1,p2,…,pk 的美观度(beauty)为:使用任意次数的区间排序操作将其排序所需的最短时间。每次区间排序操作执行如下步骤:
- 选择两个整数 l 和 r(满足 1≤l<r≤k);
- 将子数组 pl,pl+1,…,pr 进行排序,耗时 r−l 秒。
请计算数组 a 的所有子数组的美观度之和。
数组的一个子数组定义为该数组中连续的一段元素所构成的序列。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤3⋅105) — the length of the array a.
The second line of each test case consists of n integers a1,a2,…,an (1≤ai≤109). It is guaranteed that all elements of a are pairwise distinct.
It is guaranteed that the sum of n over all test cases does not exceed 3⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤3⋅105)—— 数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)。保证数组 a 中所有元素两两不同。
保证所有测试用例的 n 值之和不超过 3⋅105。
输出格式
For each test case, output the sum of beauty over all subarrays of array a.
对于每个测试用例,输出数组 a 的所有子数组的“美丽值”之和。
输入输出样例
输入#1
5 2 6 4 3 3 10 6 4 4 8 7 2 5 9 8 2 4 6 12 2 6 13 3 15 5 10 8 16 9 11 18
输出#1
1 2 8 16 232
说明/提示
In the first test case:
- The subarray [6] is already sorted, so its beauty is 0.
- The subarray [4] is already sorted, so its beauty is 0.
- You can sort the subarray [6,4] in one operation by choosing l=1 and r=2. Its beauty is equal to 1.
The sum of beauty over all subarrays of the given array is equal to 0+0+1=1.
In the second test case:
- The subarray [3] is already sorted, so its beauty is 0.
- The subarray [10] is already sorted, so its beauty is 0.
- The subarray [6] is already sorted, so its beauty is 0.
- The subarray [3,10] is already sorted, so its beauty is 0.
- You can sort the subarray [10,6] in one operation by choosing l=1 and r=2. Its beauty is equal to 2−1=1.
- You can sort the subarray [3,10,6] in one operation by choosing l=2 and r=3. Its beauty is equal to 3−2=1.
The sum of beauty over all subarrays of the given array is equal to 0+0+0+0+1+1=2.
在第一个测试用例中:
- 子数组 [6] 已经有序,因此其优美值为 0。
- 子数组 [4] 已经有序,因此其优美值为 0。
- 你可以通过选择 l=1 和 r=2 对子数组 [6,4] 执行一次操作将其排序。其优美值等于 1。
给定数组所有子数组的优美值之和为 0+0+1=1。
在第二个测试用例中:
- 子数组 [3] 已经有序,因此其优美值为 0。
- 子数组 [10] 已经有序,因此其优美值为 0。
- 子数组 [6] 已经有序,因此其优美值为 0。
- 子数组 [3,10] 已经有序,因此其优美值为 0。
- 你可以通过选择 l=1 和 r=2 对子数组 [10,6] 执行一次操作将其排序。其优美值等于 2−1=1。
- 你可以通过选择 l=2 和 r=3 对子数组 [3,10,6] 执行一次操作将其排序。其优美值等于 3−2=1。
给定数组所有子数组的优美值之和为 0+0+0+0+1+1=2。
输入解题思路,AI测评打分。不知道怎么写?