Codeforces Round 1039 (Div. 2)
D
Sum of LDS
Codeforces
1600
原题
← CE1 →
翻译中…

You're given a permutation$^{\text{∗}}$ $p_1, \ldots, p_n$ such that $\max(p_i, p_{i+1}) \gt p_{i+2}$ for all $1 \leq i \leq n-2$.

翻译中…

Compute the sum of the length of the longest decreasing subsequence$^{\text{†}}$ of the subarray $[p_l, p_{l+1}, \ldots, p_r]$ over all pairs $1 \leq l \leq r \leq n$.

翻译中…

$^{\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{†}}$Given an array $b$ of size $|b|$, a decreasing subsequence of length $k$ is a sequence of indices $i_1, \ldots, i_k$ such that: $1 \leq i_1 \lt i_2 \lt \ldots \lt i_k \leq |b|$ $b_{i_1} \gt b_{i_2} \gt \ldots \gt b_{i_k}$

翻译中…

Input

翻译中…

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

翻译中…

The first line of each test case contains a single integer $n$ ($3 \leq n \leq 500\,000$).

翻译中…

The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, $p_i$ are pairwise distinct).

翻译中…

It is guaranteed that $\max(p_i, p_{i+1}) \gt p_{i+2}$ for all $1 \leq i \leq n-2$.

翻译中…

The sum of $n$ over all test cases does not exceed $500\,000$.

翻译中…

Output

翻译中…

For each test case, output the sum over all subarrays of the length of its longest decreasing subsequence.

翻译中…

Note

翻译中…

For any array $a$, we define $\text{LDS}(a)$ as the length of the longest decreasing subsequence of $a$.

翻译中…

In the first test case, all subarrays are decreasing.

翻译中…

In the second one, we have

翻译中…

$\text{LDS}([4]) = \text{LDS}([3]) = \text{LDS}([1]) = \text{LDS}([2]) = 1$

翻译中…

$\text{LDS}([4,3]) = \text{LDS}([3,1]) = 2, \text{LDS}([1, 2]) = 1$

翻译中…

$\text{LDS}([4,3,1]) = 3, \text{LDS}([3,1,2]) = 2$

翻译中…

$\text{LDS}([4,3,1,2]) = 3$

翻译中…

So the answer is $1+1+1+1+2+2+1+3+2+3=17$.

翻译中…

样例

Input #1
4
3
3 2 1
4
4 3 1 2
6
6 1 5 2 4 3
3
2 3 1
Output #1
10
17
40
8
InputCopy OutputCopy
题目信息
题号
2128D
时间限制
2 s
内存限制
256 MB
标签
暴力
组合数学
动态规划
贪心
数学

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