题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520
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 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.
For each case, print the answer in one line.
3 5 1 2 3 4 5 3 2 3 3 4 2 3 3 2
105 21 38
题意:
给出一个集合,求每个子集合非重复元素的总和。
PS:
dp += (i-w[x])*x;
dp 表示当前输入的x前(包含x)的子序列的和;
每次新加一个x,那么他和上一次出现的同一个x之间的就是新增的,
w[x] = i; 记录的是前面一次x最后出现的位置;
模拟一下就知道了:
输入 1 2 3
dp 1 5 14
sum 1 6 20
w[i] 1 2 3
代码如下:
#include <cstdio>
#include <cstring>
int main()
{
int t;
int n;
int w[100017];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int x;
memset(w,0,sizeof(w));
long long sum = 0, dp = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d",&x);
dp += (i-w[x])*x;
sum+=dp;
w[x] = i;
}
printf("%lld\n",sum);
}
return 0;
}
原文:http://blog.csdn.net/u012860063/article/details/45343683