F - Beauty of Array

F - Beauty of Array

https://vjudge.net/contest/225989#problem/F

Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A.


Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (1 <= N <= 100000), which indicates the size of the array. The next line contains N positive integers separated by spaces. Every integer is no larger than 1000000.

Output

For each case, print the answer in one line.

Sample Input
3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2
Sample Output
105
21
38

[The gist of the title] Define the beauty number as the sum of all different numbers in a sequence, find the beauty sum of all word sequences in a sequence

  1 <= N <= 100000


[ Problem- solving ideas] Because the data is relatively large, the conventional method to find the sum of the word sequence will definitely not work. We might as well think this way: because it is different from different numbers

, it can be seen that the numbers in the sequence are added one by one, each time a number is added, and the position of the newly added number in the previous sequence is counted for the first time, which is not well expressed.

for example:

1 2 3

Define dp (the sum of all word sequences that contain itself in front of the current element (including itself))

Define sum (the sum of all word sequences preceding the current element, including this element)

//input 1 2 3

//c      1     5     14

//sum   1     6      20

//a[i]      1     2      3


   AC code:

# include <stdio.h>
# include <string.h>

int a[100001];

int main(void)
{
	int t, b, i, e;
	long long sum, c;
	scanf("%d", &t);
	while (t --)
	{
		memset(a, 0, sizeof(a));
		scanf("%d", &b);
		sum = 0, c = 0;
		for (i = 1; i <= b; i ++)
		{
			scanf("%d", &e);
			c += (i - a[e])*e;
			sum += c;
			a[e] = i;
		}
		printf("%lld\n", sum);
	}
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325815902&siteId=291194637