#P4065. [JXOI2017] 颜色

    ID: 3006 远端评测题 3000ms 500MiB 尝试: 1 已通过: 1 难度: 8 上传者: 标签>2017线段树各省省选枚举,暴力概率论,统计

[JXOI2017] 颜色

Description

Kelian has a sequence of positive integers of length nn, denoted by AiA_i, where equal integers represent the same color.

Kelian thinks the sequence is too long, so she decides to choose some colors and delete all positions of those colors.

Deleting color ii is defined as removing from the sequence all positions jj such that Aj=iA_j = i.

However, sometimes after deletions, the entire sequence breaks into several segments. Kelian does not like that, so she wants to know how many ways to delete colors will make the remaining sequence non-empty and contiguous.

For example, for the color sequence {1,2,3,4,5}\{1, 2, 3, 4, 5\}, after deleting color 33, the sequence becomes two segments {1,2}\{1, 2\} and {4,5}\{4, 5\}, which does not satisfy the requirement. After deleting color 11, the sequence becomes {2,3,4,5}\{2, 3, 4, 5\}, which satisfies the requirement.

Two schemes are different if and only if there exists at least one color ii that is deleted in exactly one of them.

Input Format

The first line contains an integer TT, the number of test cases.

For each test case, the first line contains an integer nn, the length of the sequence; the second line contains nn integers describing the color sequence.

Output Format

For each test case, output a single integer denoting the answer.

1
5
1 3 2 4 3
6

Hint

The valid deletion schemes are $\{1\}, \{1, 3\}, \{1, 2, 3\}, \{1, 3, 4\}, \{2, 3, 4\}, \varnothing$.

For 20%20\% of the testdata, it is guaranteed that 1n201 \le \sum n \le 20.

For 40%40\% of the testdata, it is guaranteed that 1n5001 \le \sum n \le 500.

For 60%60\% of the testdata, it is guaranteed that 1n1041 \le \sum n \le 10^4.

For 100%100\% of the testdata, it is guaranteed that 1T,n3×105,1Ain1 \le T, \sum n \le 3 \times 10^5, 1 \le A_i \le n.

Statement fixed by @Starrykiller.\text{Statement fixed by @Starrykiller.}

Translated by ChatGPT 5