CF2268D.AghaBalaSar and Hamed

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a permutation∗^{\text{∗}} pp of length nn.

For each index ii, you may move in one step to:

  • any index j<ij \lt i, or
  • the first index j>ij \gt i such that pj>pip_j \gt p_i (if such an index exists),

In other words:

  • 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)(i,j), let f(i,j)f(i,j) be the minimum number of steps needed to move from ii to jj. Note that if we cannot reach jj from ii, then f(i,j)=0f(i,j)=0.

Your task is to compute:

∑1≤i,j≤nf(i,j).\sum_{1 \le i,j \le n} f(i,j).

∗^{\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{∗}} pp。

对于每个下标 ii,你可以在一步内移动到以下位置之一:

  • 任意满足 j<ij \lt i 的下标 jj,或
  • 满足 j>ij \gt i 且 pj>pip_j \gt p_i 的最小下标 jj(若这样的下标存在),

换言之:

  • 你总可以向左移动到任意位置;
  • 向右移动时,你只能移动到值严格大于当前位置值的、距离最近的右侧位置。

对每一对下标 (i,j)(i,j),记 f(i,j)f(i,j) 为从 ii 移动到 jj 所需的最少步数。注意:若无法从 ii 到达 jj,则定义 f(i,j)=0f(i,j)=0。

你的任务是计算:

∑1≤i,j≤nf(i,j). \sum_{1 \le i,j \le n} f(i,j).

∗^{\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 \leq n \leq 10^6) — the length of pp.

The second line of each test case contains nn distinct integers p1,p2,…,pnp_1,p_2,\ldots,p_n (1≤pi≤n1\le p_i\le n) — the elements of pp.

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 \leq n \leq 10^6)—— 表示排列 pp 的长度。

每个测试用例的第二行包含 nn 个互不相同的整数 p1,p2,…,pnp_1,p_2,\ldots,p_n(1≤pi≤n1\le p_i\le n)—— 即排列 pp 的元素。

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

输出格式

For each test case, print a single integer — the value of ∑1≤i,j≤nf(i,j)\sum\limits_{1 \le i,j \le n} f(i,j).

对于每个测试用例,输出一个整数——即 ∑1≤i,j≤nf(i,j)\sum\limits_{1 \le i,j \le n} f(i,j) 的值。

输入输出样例

  • 输入#1

    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

    输出#1

    2
    1
    4
    7
    15
    38

说明/提示

In the first test case, f(1,2)+f(2,1)=1+1=2f(1, 2) + f(2, 1) = 1 + 1 = 2.

In the second test case, f(1,2)+f(2,1)=0+1=1f(1, 2) + f(2, 1) = 0 + 1 = 1.

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=4f(1, 2) + f(1, 3) + f(2, 1) + f(2, 3) + f(3, 1) + f(3, 2) = 1 + 0 + 1 + 0 + 1 + 1 = 4.

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=7f(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(2,1)=1+1=2f(1, 2) + f(2, 1) = 1 + 1 = 2。

在第二个测试用例中,f(1,2)+f(2,1)=0+1=1f(1, 2) + f(2, 1) = 0 + 1 = 1。

在第三个测试用例中,f(1,2)+f(1,3)+f(2,1)+f(2,3)+f(3,1)+f(3,2)=1+0+1+0+1+1=4f(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+2+1+1+1+1=7f(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测评打分。不知道怎么写?

首页