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 tt and nn.

You are given an array aa, consisting of nn distinct integers a1,a2,…,ana_1, a_2, \ldots, a_n.

Define the beauty of an array p1,p2,…pkp_1, p_2, \ldots p_k 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 ll and rr (1≤l<r≤k1 \le l \lt r \le k).
  • Sort the subarray pl,pl+1,…,prp_l, p_{l + 1}, \ldots, p_r in r−lr - l seconds.

Please calculate the sum of beauty over all subarrays of array aa.

A subarray of an array is defined as a sequence of consecutive elements of the array.

此题与简单版本的唯一区别在于 tt 和 nn 的约束条件。

给你一个由 nn 个互不相同的整数 a1,a2,…,ana_1, a_2, \ldots, a_n 构成的数组 aa。

定义数组 p1,p2,…,pkp_1, p_2, \ldots, p_k 的美观度(beauty)为:使用任意次数的区间排序操作将其排序所需的最短时间。每次区间排序操作执行如下步骤:

  • 选择两个整数 ll 和 rr(满足 1≤l<r≤k1 \le l \lt r \le k);
  • 将子数组 pl,pl+1,…,prp_l, p_{l + 1}, \ldots, p_r 进行排序,耗时 r−lr - l 秒。

请计算数组 aa 的所有子数组的美观度之和。

数组的一个子数组定义为该数组中连续的一段元素所构成的序列。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

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

The second line of each test case consists of nn integers a1,a2,…,ana_1,a_2,\ldots, a_n (1≤ai≤1091\le a_i\le 10^9). It is guaranteed that all elements of aa are pairwise distinct.

It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

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

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

保证所有测试用例的 nn 值之和不超过 3⋅1053 \cdot 10^5。

输出格式

For each test case, output the sum of beauty over all subarrays of array aa.

对于每个测试用例,输出数组 aa 的所有子数组的“美丽值”之和。

输入输出样例

  • 输入#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][6] is already sorted, so its beauty is 00.
  • The subarray [4][4] is already sorted, so its beauty is 00.
  • You can sort the subarray [6,4][6, 4] in one operation by choosing l=1l = 1 and r=2r = 2. Its beauty is equal to 11.

The sum of beauty over all subarrays of the given array is equal to 0+0+1=10 + 0 + 1 = 1.

In the second test case:

  • The subarray [3][3] is already sorted, so its beauty is 00.
  • The subarray [10][10] is already sorted, so its beauty is 00.
  • The subarray [6][6] is already sorted, so its beauty is 00.
  • The subarray [3,10][3, 10] is already sorted, so its beauty is 00.
  • You can sort the subarray [10,6][10, 6] in one operation by choosing l=1l = 1 and r=2r = 2. Its beauty is equal to 2−1=12 - 1 = 1.
  • You can sort the subarray [3,10,6][3, 10, 6] in one operation by choosing l=2l = 2 and r=3r = 3. Its beauty is equal to 3−2=13 - 2 = 1.

The sum of beauty over all subarrays of the given array is equal to 0+0+0+0+1+1=20 + 0 + 0 + 0 + 1 + 1 = 2.

在第一个测试用例中:

  • 子数组 [6][6] 已经有序,因此其优美值为 00。
  • 子数组 [4][4] 已经有序,因此其优美值为 00。
  • 你可以通过选择 l=1l = 1 和 r=2r = 2 对子数组 [6,4][6, 4] 执行一次操作将其排序。其优美值等于 11。

给定数组所有子数组的优美值之和为 0+0+1=10 + 0 + 1 = 1。

在第二个测试用例中:

  • 子数组 [3][3] 已经有序,因此其优美值为 00。
  • 子数组 [10][10] 已经有序,因此其优美值为 00。
  • 子数组 [6][6] 已经有序,因此其优美值为 00。
  • 子数组 [3,10][3, 10] 已经有序,因此其优美值为 00。
  • 你可以通过选择 l=1l = 1 和 r=2r = 2 对子数组 [10,6][10, 6] 执行一次操作将其排序。其优美值等于 2−1=12 - 1 = 1。
  • 你可以通过选择 l=2l = 2 和 r=3r = 3 对子数组 [3,10,6][3, 10, 6] 执行一次操作将其排序。其优美值等于 3−2=13 - 2 = 1。

给定数组所有子数组的优美值之和为 0+0+0+0+1+1=20 + 0 + 0 + 0 + 1 + 1 = 2。

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

首页