Codeforces Round 1082 (Div. 1)
A2
Lost Civilization (Hard Version)
Codeforces
1700
原题
← A1B →
翻译中…

This is the hard version of the problem. The difference between the versions is that in this version, you must compute the sum of values over all subsegments. You can hack only if you solved all versions of this problem.

翻译中…

Let's define an algorithm to generate a sequence of $m+k$ integers as follows:

翻译中…
  1. First, receive a sequence $x$ of $m$ integers as input. If $k=0$, terminate immediately and return the sequence $x$.
  2. Then, select any index $1 \le i \le |x|$ and insert $(x_i+1)$ immediately after the element $x_i$.
  3. If $x$ contains exactly $m+k$ integers, terminate and return the sequence $x$. Otherwise, return to the second step.
翻译中…

Alice knows that this algorithm was used by an ancient civilization in order to hide their secrets safely. Alice wants to learn the knowledge that they wanted to hide, but it is not an easy job to infer the input from the output of the algorithm.

翻译中…

For a sequence $b$ of $n$ integers, let us define $f(b)$ as the length of the shortest sequence that could be given as an input for the algorithm to generate $b$.

翻译中…

Given a sequence $a$ of $n$ integers, please compute the value of the following sum

翻译中…

$$\sum_{l=1}^n {\sum_{r=l}^n {f([a_l,a_{l+1},\ldots,a_r])}}$$

翻译中…

In other words, you must find the sum of $f(c)$ over all subsegments$^{\text{∗}}$ $c$ of $a$.

翻译中…

$^{\text{∗}}$A sequence $a$ is a subsegment of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Two subsegments are considered different if the sets of positions of the deleted elements are different.

翻译中…

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 \le 300\,000$).

翻译中…

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

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $300\,000$.

翻译中…

Output

翻译中…

For each test case, output the answer on a separate line.

翻译中…

Note

翻译中…

In the first test case, all subsegments of $a=[1,2,3,4,5]$ can be generated from a sequence of length $1$.

翻译中…

In the second test case, all subsegments of $a=[1,3,5,7,9]$ cannot be generated from any shorter sequence than itself.

翻译中…

样例

Input #1
5
5
1 2 3 4 5
5
1 3 5 7 9
5
1 2 5 6 5
7
1 2 4 5 3 7 8
9
9 8 9 2 3 4 4 5 3
Output #1
15
35
25
60
78
InputCopy OutputCopy
题目信息
题号
2201A2
时间限制
2 s
内存限制
256 MB
标签
数据结构
动态规划

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