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<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define mod(x) ((x)%M)
#define pi acos(-1.0)
#define rep(i,x,n) for(int i=(x); i<(n); i++)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6+10;
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[2]={-1,1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
LL mapp[1100000],ans,sum;
int a[110000];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
sum=0;
memset(mapp,-1,sizeof(mapp));
for(int i=0; i<n; i++)
{
scanf("%d",a+i);
if(i==0)ans=a[i];
else ans+=(i-mapp[a[i]])*a[i];
mapp[a[i]]=i;
sum += ans;
}
printf("%lld
",sum);
}
return 0;
}
/*
【题意】
我们定义一个序列的贡献是它里面所有不相同的数字之和,然后给出一个数字序列,求其所有连续子序列的贡献和。
【类型】计数DP
【分析】
假设当前给出序列为: 2 3 3 2
以第一个 2 结尾的子串有一个,当前贡献为 1*2=2
以第一个 3 结尾的子串有两个,当前贡献为 2+2*3=8
以第二个 3 结尾的子串有三个,因为 3 已经在第二个位置出现过,因此对于包含前一个 3 的所有子串的贡献都已经计算过了,当前贡献为 8+(3-2)*3=11
以第二个 2 结尾的子串有四个,因为 2 已经在第一个位置出现过,因此对于包含前一个 2 的所有子串的贡献也计算过了,当前贡献为 11+(4-1)*2=17
最终的结果是所有计算的数值之和。
【时间复杂度&&优化】
【trick】
【数据】
*/