7017.Distinct Values Sum

Time Limit: 1s Memory Limit: 512MB

You are given an array $x_1,x_2,\dots,x_n$ . Let $d(a,b)$ denote the number of distinct values in the subarray $x_a,x_{a+1},\dots,x_b$ .

Your task is to calculate the sum $\sum_{a=1}^n \sum_{b=a}^n d(a,b)$ , i.e., the sum of $d(a,b)$ for all subarrays.

Input Format(From the terminal/stdin)

The first line has an integer $n$ : the array size.

The next line has $n$ integers $x_1,x_2,\dots,x_n$ : the array contents.

  • $1 \le n \le 2 \cdot 10^5$
  • $1 \le x_i \le 10^9$

Output Format(To the terminal/stdout)

Print one integer: the required sum.

Sample Input

Copy
5
1 2 3 1 1
 \n
 · · · · \n

Sample Output

Copy
29
  \n

In this array, $6$ subarrays have $1$ distinct value, $4$ subarrays have $2$ distinct values and $5$ subarrays have $3$ distinct values. Thus, the sum is $6\cdot1+4\cdot2+5\cdot3=29$ .

Source: CSES, Additional Problems I, 3150

Submit

请先 登录

© 2025 FAQs