Codeforces Round 945 (Div. 2)
E
Cat, Fox and Swaps
Codeforces
2500
原题
← DF →
翻译中…

Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.

翻译中…

They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$.

翻译中…

You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$).

翻译中…

$^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

翻译中…

Input

翻译中…

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

翻译中…

Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$).

翻译中…

The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

翻译中…

Output

翻译中…

For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.

翻译中…

Note

翻译中…

In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$.

翻译中…

In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.

翻译中…

样例

Input #1
7
2
2 1
3
3 1 2
4
3 2 1 4
5
5 3 1 2 4
5
1 2 3 4 5
6
3 2 1 5 4 6
6
1 3 2 4 5 6
Output #1
6
11
23
29
55
46
58
InputCopy OutputCopy
题目信息
题号
1973E
时间限制
2 s
内存限制
256 MB
标签
图论
数学
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。