Codeforces Round 1064 (Div. 1)
F
Arctic Acquisition
Codeforces
3500
原题
← E→
翻译中…

You are given a permutation$^{\text{∗}}$ $a_1,a_2,\ldots,a_n$ of length $n$.

翻译中…

An interval $[l,r]$ ($1\le l\le r\le n$) is jagged if and only if it contains a 21435-subsequence; that is, there exist integers $i_1,i_2,i_3,i_4,i_5$ such that $l\le i_1 \lt i_2 \lt i_3 \lt i_4 \lt i_5\le r$, and $a_{i_2} \lt a_{i_1} \lt a_{i_4} \lt a_{i_3} \lt a_{i_5}$.

翻译中…

Your task is to calculate how many of the $\frac{n(n+1)}2$ intervals are jagged.

翻译中…

$^{\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).

翻译中…

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.

翻译中…

The first line of each test case contains a single integer $n$ ($1\le n\le10^6$) — the length of the permutation.

翻译中…

The second line of each test case contains $n$ distinct integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$).

翻译中…

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

翻译中…

Output

翻译中…

For each test case, output the number of jagged subarrays.

翻译中…

Note

翻译中…

In the first test case, the only jagged subarray is $[1,5]$, containing $[2,1,4,3,5]$ as a subsequence.

翻译中…

In the third test case, the subarray $[1,8]$ is jagged because it contains $[9,6,11,10,13]$ as a subsequence, which is a 21435-subsequence.

翻译中…

样例

Input #1
5
5
2 1 4 3 5
10
10 3 5 2 1 4 9 8 6 7
15
3 9 15 6 11 10 5 13 12 7 4 8 14 1 2
12
10 7 12 5 4 1 2 9 3 8 6 11
30
22 30 7 17 4 13 26 28 24 20 2 11 27 21 5 19 9 10 23 14 1 25 6 8 3 18 29 12 16 15
Output #1
1
0
28
5
185
InputCopy OutputCopy
题目信息
题号
2165F
时间限制
5 s
内存限制
1024 MB
标签
数据结构
贪心

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