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.
There are multiple test cases. The first line of input contains an integer Tindicating 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="">For each case, print the answer in one line.
<h4< dd="">3 5 1 2 3 4 5 3 2 3 3 4 2 3 3 2<h4< dd="">
105 21 38
题意:
给你N个数让你求他的连续子序列的和若一个连续子序列中出现重复项就不算。。子序列2 3 3 的值为5
eg:
1 2 3
dp是从前慢慢往后走的。。。别急啊。。
dp[1]=包含 1 :1
dp[2]=包含 2 : 2 | 1 2
dp[3]=包含 3 : 3 | 2 3 | 1 2 3
ans=dp[1]+dp[2]+dp[3];
dp[i]=a[i]*i+dp[i-1];///i*a[i]代表a[i]出现i次的总和(因为能和前边的i-1个数组合和自己本身一次就是i次组合了)
至于重复的呢
2 3 2 3
我们直接看A【4】=3
dp[4]=3 | 2 3| 3 2 3(重复去掉)| (2) (3) 2 3(重复去掉)
重复的时候减去。。
dp[4]=dp[3]+a[i]*i- val[a[i]]*a[i];
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
long long dp[100003];
int v[1000003];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
memset(v,0,sizeof(v));
scanf("%d",&n);
long long ans=0;
dp[0]=0;
for(int i=1;i<=n;i++)
{
int a;
cin>>a;
if(v[a]==0)
dp[i]=dp[i-1]+a*i;
else dp[i]=dp[i-1]+a*i-a*v[a];
v[a]=i;
ans+=dp[i];
}
printf("%lld\n",ans);
}
}