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.h>
using namespace std;
#define ll long long
const int maxn=100007;
ll dp[maxn],d[maxn],sum[maxn];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
int n;scanf("%d",&n);
memset(d,0,sizeof(d));
ll x;scanf("%lld",&x);
sum[1]=dp[1]=x;d[x]=1;
for(ll i=2;i<=n;i++)
{
scanf("%lld",&x);
if(d[x])
{
ll id=d[x];d[x]=i;
sum[i]=sum[i-1]+(i-id)*x;
dp[i]=dp[i-1]+sum[i];
}
else
{
d[x]=i;
sum[i]=sum[i-1]+i*x;
dp[i]=dp[i-1]+sum[i];
}
}
printf("%lld\n",dp[n]);
}
return 0;
}

本文探讨了一个有趣的问题:如何计算一个整数数组中所有连续子数组的美丽之和。美丽定义为数组中所有不同整数的总和。通过巧妙的数据结构和算法设计,本文提供了一种高效的解决方案。
1万+

被折叠的 条评论
为什么被折叠?



