Seeking a sequence of all sections (the number of different numbers in the interval), and

Seeking a sequence of all sections (the number of different numbers in the interval), and

Links: https://ac.nowcoder.com/acm/contest/888/B
Source: Cattle-off network

Gromah and LZR have entered the second level. There is a sequence a1,a2,⋯ ,an on the wall.

There is also a note board saying "the beauty value of a sequence is the number of different elements in the sequence".

LZR soon comes up with the password of this level, which is the sum of the beauty values of all successive subintervals of the sequence on the wall.

Please help them determine the password!

Enter a description:

The first line contains one positive integer nn_{}n, denoting the length of the sequence.

The second line contains nn_{}n positive integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_na1,a2,⋯,an, denoting the sequence.

1 ≤ \(a_i\) ≤ n ≤ \(10^5\)

Output Description:

Print a non-negative integer in a single line, denoting the answer.示例1

Entry

4
1 2 1 3

Export

18

说明
The beauty values of subintervals [1,1],[2,2],[3,3],[4,4] are all 1.
The beauty values of subintervals [1,2],[1,3],[2,3],[3,4] are all 2.
The beauty values of subintervals [1,4],[2,4] are all 3.
As a result, the sum of all beauty values are 1×4+2×4+3×2=18

The meaning of problems

Seeking a sequence of all sections (the number of different numbers in the interval), and

answer

Recursive recording \ (a_i \) where they appear, if \ (a_i \) position occurs before the CNT [ \ (a_i \) ], then the interval [L, i] and [L, i-1] ( 0 <L <[= CNT \ (a_i \) ]) the result is the same, [L, i] than [L, I-1] (CNT [ \ (a_i \) ] <L <= I) over, because in these intervals, \ (a_i \) belongs to a different number.

#include <cstdio>
int cnt[100010];
int main() {
    int n, a;
    scanf("%d", &n);
    long long sum[100010], ans = 0;
    sum[0] = 0;
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a);
        sum[i] = sum[i-1] + (i - cnt[a]);
        cnt[a] = i;
        ans += sum[i];
    }
    printf("%lld\n", ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fanshhh/p/11333158.html