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.
InputThere 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.
<h4< dd="">OutputFor each case, print the answer in one line.
<h4< dd="">Sample Input3 5 1 2 3 4 5 3 2 3 3 4 2 3 3 2<h4< dd="">Sample Output
105 21 38
2 3 3
3 =3
2 3 =5
3 3 =3
2 3 3 =5
此种类型的题首先要知道一定是有规律可循的
2 3 4 5
3 4 5
4 5
5
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
long long s1[1000005],w[1000005];
int main()
{
long long n,a,b,c;
cin>>a;
while(a--){
cin>>n;
memset(w,0,sizeof(w));
long long sum=0,p=0;
for(int i=1;i<=n;i++)
{
cin>>s1[i];
p=p+s1[i]*(i-w[s1[i]]);//第二个p是上一个新增元素产生的新增连续子集的总值; 第一个p为当前新增元素产生的新增连续子集的值
sum+=p;
w[s1[i]]=i;//此处用到了桶排序,记录当前元素最后一次出现的位置
}
cout<<sum<<endl;
}
}