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\n",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】 【数据】
*/

ZOJ 3872 Beauty of Array【无重复连续子序列的贡献和/规律/DP】的更多相关文章

  1. ZOJ 3872 Beauty of Array

    /** Author: Oliver ProblemId: ZOJ 3872 Beauty of Array */ /* 需求: 求beauty sum,所谓的beauty要求如下: 1·给你一个集合 ...

  2. DP ZOJ 3872 Beauty of Array

    题目传送门 /* DP:dp 表示当前输入的x前的包含x的子序列的和, 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: sum 表示当前输入x前的所有和: a[x] 表示id: 详细解释 ...

  3. ZOJ 3872 Beauty of Array 连续子序列求和

    Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...

  4. ZOJ 3872 Beauty of Array DP 15年浙江省赛D题

    也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉 What a pity... 题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 ...

  5. ZOJ 3872 Beauty of Array (The 12th Zhejiang Provincial Collegiate Programming Contest )

    对于没有题目积累和clever mind的我来说,想解这道题还是非常困难的,也根本没有想到用dp. from: http://blog.csdn.net/u013050857/article/deta ...

  6. Zoj 3842 Beauty of Array

    Problem地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 根据题目的要求,需要算出所有连续子数组的the be ...

  7. lintcode-397-最长上升连续子序列

    397-最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 注意事 ...

  8. 动态规划(Dynamic Programming, DP)---- 最大连续子序列和

    动态规划(Dynamic Programming, DP)是一种用来解决一类最优化问题的算法思想,简单来使,动态规划是将一个复杂的问题分解成若干个子问题,或者说若干个阶段,下一个阶段通过上一个阶段的结 ...

  9. ACM-DP之最大连续子序列——hdu1231

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

随机推荐

  1. [LeetCode] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

  2. HUST 1103 校赛 邻接表-拓扑排序

    Description N students were invited to attend a party, every student has some friends, only if someo ...

  3. Linux和windows下检查jsp后门文件的方法

    Linux下: find . -name "*.jsp" | xargs egrep -liw "createNewFile| File\(| File |applica ...

  4. GXC 钱包部署

    参考: [ 官方 wiki ] 基于 Ubuntu 的 GXC 部署 基础环境 OS: Ubuntu gxc: 官方 [ release 最新版本 ] 下载 release 包(ubuntu) cd ...

  5. 从C语言项目谈编程

    很多初学C语言的小伙伴,在学习之初并没有一个大概的概念,学习这门语言需要掌握多少知识点,怎么才算学的差不多? C语言的精髓点在哪? 学到多少东西才能够达到做项目的标准?学习的时候需要注意哪些细节点?疑 ...

  6. php中比较好用的函数

    PHP中一个好用的函数parse_url,特别方便用来做信息抓取的分析,举例子如下: $url = "http://www.electrictoolbox.com/php-extract-d ...

  7. HDU 1574 RP问题 (dp)

    题目链接 Problem Description 在人类社会中,任何个体都具有人品,人品有各种不同的形式,可以从一种形式转换为另一种形式,从一个个体传递给另一个个体,在转换和传递的过程中,人品不会消失 ...

  8. 如何用js自己实现Animate运动函数

    js运动是我们学习js必不可少的研究部分,首先我们要知道js的运动其实仅仅是不断改变元素的某个属性值而已,比如不断改变一个绝对定位div的left值,那么你看到的效果就是这个div不断的向右边运动,那 ...

  9. python基础===isinstance() 函数,判断一个对象是否是一个已知的类型

    isinstance(object, classinfo) object -- 实例对象. classinfo -- 可以是直接或间接类名.基本类型或者有它们组成的元组. >>>a ...

  10. python之requests库使用问题汇总

    一.请求参数类型 1.get requests.get(url, data, cookies=cookies) url:字符串: data:字典类型,可以为空: cookies:字典类型,可以为空: ...