AghaBalaSar 与 HamedAI (CI翻译)
You are given a permutation$^{\text{∗}}$ $p$ of length $n$.
给定一个长度为 $n$ 的排列$^{\text{∗}}$ $p$。AI (CI翻译)
For each index $i$, you may move in one step to:
对于每个下标 $i$,你可以用一步移动到:AI (CI翻译)
- any index
$j \lt i$, or - the first index
$j \gt i$such that$p_j \gt p_i$(if such an index exists),
- 任意下标
$j \lt i$,或 - 满足
$p_j \gt p_i$的第一个下标$j \gt i$(如果这样的下标存在)。
In other words:
换句话说:AI (CI翻译)
- You can always move to any position on the left.
- On the right, you can move only to the nearest position whose value is strictly greater than the current one.
- 你始终可以移动到左侧的任意位置。
- 向右移动时,你只能移动到值严格大于当前位置的值的最近位置。
For every pair of indices $(i,j)$, let $f(i,j)$ be the minimum number of steps needed to move from $i$ to $j$. Note that if we cannot reach $j$ from $i$, then $f(i,j)=0$.
对于每一对下标 $(i,j)$,令 $f(i,j)$ 表示从 $i$ 移动到 $j$ 所需的最少步数。注意,如果无法从 $i$ 到达 $j$,则 $f(i,j)=0$。AI (CI翻译)
Your task is to compute:
你的任务是计算:AI (CI翻译)
$$\sum_{1 \le i,j \le n} f(i,j).$$
$$\sum_{1 \le i,j \le n} f(i,j).$$AI (CI翻译)
$^{\text{∗}}$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).
$^{\text{∗}}$长度为 $n$ 的排列是一个由 $1$ 到 $n$ 这 $n$ 个互不相同的整数按任意顺序组成的数组。例如,$[2,3,1,5,4]$ 是一个排列,但 $[1,2,2]$ 不是排列($2$ 在数组中出现了两次),$[1,3,4]$ 也不是排列($n=3$,但数组中出现了 $4$)。AI (CI翻译)
Input
输入格式
AI (CI翻译)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.
每个测试包含多组测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 10^4$)。接下来是各组测试用例的描述。AI (CI翻译)
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^6$) — the length of $p$.
每组测试用例的第一行包含一个整数 $n$($1 \leq n \leq 10^6$)——$p$ 的长度。AI (CI翻译)
The second line of each test case contains $n$ distinct integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$) — the elements of $p$.
每组测试用例的第二行包含 $n$ 个互不相同的整数 $p_1,p_2,\ldots,p_n$($1\le p_i\le n$)——$p$ 的元素。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
保证所有测试用例的 $n$ 之和不超过 $10^6$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, print a single integer — the value of $\sum\limits_{1 \le i,j \le n} f(i,j)$.
对于每组测试用例,输出一个整数——$\sum\limits_{1 \le i,j \le n} f(i,j)$ 的值。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, $f(1, 2) + f(2, 1) = 1 + 1 = 2$.
在第一组测试用例中,$f(1, 2) + f(2, 1) = 1 + 1 = 2$。AI (CI翻译)
In the second test case, $f(1, 2) + f(2, 1) = 0 + 1 = 1$.
在第二组测试用例中,$f(1, 2) + f(2, 1) = 0 + 1 = 1$。AI (CI翻译)
In the third test case, $f(1, 2) + f(1, 3) + f(2, 1) + f(2, 3) + f(3, 1) + f(3, 2) = 1 + 0 + 1 + 0 + 1 + 1 = 4$.
在第三组测试用例中,$f(1, 2) + f(1, 3) + f(2, 1) + f(2, 3) + f(3, 1) + f(3, 2) = 1 + 0 + 1 + 0 + 1 + 1 = 4$。AI (CI翻译)
In the fourth test case, $f(1, 2) + f(1, 3) + f(2, 1) + f(2, 3) + f(3, 1) + f(3, 2) = 1 + 2 + 1 + 1 + 1 + 1 = 7$.
在第四组测试用例中,$f(1, 2) + f(1, 3) + f(2, 1) + f(2, 3) + f(3, 1) + f(3, 2) = 1 + 2 + 1 + 1 + 1 + 1 = 7$。AI (CI翻译)
样例
6
2
1 2
2
2 1
3
1 3 2
3
1 2 3
5
1 3 5 2 4
7
6 2 4 3 7 5 1
2
1
4
7
15
38
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。