CF2165F.Arctic Acquisition

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are given a permutation∗^{\text{∗}} a1,a2,…,ana_1,a_2,\ldots,a_n of length nn.

An interval [l,r][l,r] (1≤l≤r≤n1\le l\le r\le n) is jagged if and only if it contains a 21435-subsequence; that is, there exist integers i1,i2,i3,i4,i5i_1,i_2,i_3,i_4,i_5 such that l≤i1<i2<i3<i4<i5≤rl\le i_1 \lt i_2 \lt i_3 \lt i_4 \lt i_5\le r, and ai2<ai1<ai4<ai3<ai5a_{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 n(n+1)2\frac{n(n+1)}2 intervals are jagged.

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

给你一个长度为 nn 的排列∗^{\text{∗}} a1,a2,…,ana_1,a_2,\ldots,a_n。

区间 [l,r][l,r](其中 1≤l≤r≤n1\le l\le r\le n)被称为锯齿形的(jagged),当且仅当它包含一个 21435-子序列;即存在整数 i1,i2,i3,i4,i5i_1,i_2,i_3,i_4,i_5,满足 l≤i1<i2<i3<i4<i5≤rl\le i_1 \lt i_2 \lt i_3 \lt i_4 \lt i_5\le r,且

ai2<ai1<ai4<ai3<ai5.a_{i_2} \lt a_{i_1} \lt a_{i_4} \lt a_{i_3} \lt a_{i_5}.

你的任务是计算在全部 n(n+1)2\frac{n(n+1)}{2} 个区间中,有多少个是锯齿形的。

∗^{\text{∗}} 长度为 nn 的排列是指由 11 到 nn 中互不相同的 nn 个整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

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

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

The second line of each test case contains nn distinct integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤n1\le a_i\le n).

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1061\le n\le10^6)—— 表示排列的长度。

每个测试用例的第二行包含 nn 个互不相同的整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤n1\le a_i\le n)。

保证所有测试用例的 nn 值之和不超过 10610^6。

输出格式

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

对于每个测试用例,输出锯齿形子数组的数量。

输入输出样例

  • 输入#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

    输出#1

    1
    0
    28
    5
    185

说明/提示

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

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

在第一个测试用例中,唯一的锯齿子数组是 [1,5][1,5],它包含 [2,1,4,3,5][2,1,4,3,5] 作为其子序列。

在第三个测试用例中,子数组 [1,8][1,8] 是锯齿的,因为它包含 [9,6,11,10,13][9,6,11,10,13] 作为其子序列,而该子序列是一个 21435-子序列。

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

首页