CF1676H2.Maximum Crossings (Hard Version)

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The only difference between the two versions is that in this version n≤2⋅105n \leq 2 \cdot 10^5 and the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

A terminal is a row of nn equal segments numbered 11 to nn in order. There are two terminals, one above the other.

You are given an array aa of length nn. For all i=1,2,…,ni = 1, 2, \dots, n, there should be a straight wire from some point on segment ii of the top terminal to some point on segment aia_i of the bottom terminal. You can't select the endpoints of a segment. For example, the following pictures show two possible wirings if n=7n=7 and a=[4,1,4,6,7,7,5]a=[4,1,4,6,7,7,5].

A crossing occurs when two wires share a point in common. In the picture above, crossings are circled in red.

What is the maximum number of crossings there can be if you place the wires optimally?

两个版本的唯一区别在于,本版本中 n≤2⋅105n \leq 2 \cdot 10^5,且所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

终端是一行 nn 个等长线段,按顺序编号为 11 至 nn。共有两个终端,一个位于另一个正上方。

给定一个长度为 nn 的数组 aa。对每个 i=1,2,…,ni = 1, 2, \dots, n,需从上侧终端的第 ii 段上的某一点(不能选端点)连一条直线导线,到下侧终端的第 aia_i 段上的某一点(同样不能选端点)。例如,下图展示了当 n=7n=7 且 a=[4,1,4,6,7,7,5]a=[4,1,4,6,7,7,5] 时两种可能的布线方式:

当两条导线存在公共交点时,即产生一次交叉。上图中所有交叉点均以红色圆圈标出。

若可自由选择各导线在对应线段上的连接位置(端点除外),则最多能产生多少次交叉?

输入格式

The first line contains an integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases.

The first line of each test case contains an integer nn (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5) — the length of the array.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \leq a_i \leq n) — the elements of the array.

The sum of nn across all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000)—— 表示测试用例的数量。

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

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \leq a_i \leq n)—— 表示数组的元素。

所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the maximum number of crossings there can be if you place the wires optimally.

对于每个测试用例,输出一个整数——即在最优布线方式下,导线交叉点的最大数量。

输入输出样例

  • 输入#1

    4
    7
    4 1 4 6 7 7 5
    2
    2 1
    1
    1
    3
    2 2 2

    输出#1

    6
    1
    0
    3

说明/提示

The first test case is shown in the second picture in the statement.

In the second test case, the only wiring possible has the two wires cross, so the answer is 11.

In the third test case, the only wiring possible has one wire, so the answer is 00.

第一个测试用例如题面中的第二张图片所示。

在第二个测试用例中,唯一可能的布线方式会导致两条导线相交,因此答案为 11。

在第三个测试用例中,唯一可能的布线方式仅包含一条导线,因此答案为 00。

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

首页