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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <math.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
int _index[maxn];
int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t, n, x;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
ll sum = 0;
ll dp = 0;
//dp是以前一個結尾的前子序列和
//如果遇到多個以上,就每次更新他的最近相鄰的下標
memset(_index, 0, sizeof(_index));
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
dp += (i - _index[x]) * x;
sum += dp;
_index[x] = i;
}
printf("%lld\n", sum);
}
return 0;
}