D - Beauty of Array

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2015-04-30)

Description

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
题目描述: 求一段序列中连续子序列的和,如果这段连续子序列中有重复的话,(the summation of all distinct integers in the arra),这个序列中不相同的数的和,也就是说只计算一次。
如果要求的是一段序列中连续子序列的个数,那么如果定义d[i]为以i结尾的连续子序列的个数,d[i]=d[i-1]+1;
我们定义d[i]为以i结尾的连续子序列的和,那么如果不重复d[i]=d[i-1]+a*i;
,如果重复的话,假设1 2 3 4 5 6 7。。。。。i,如果在第j位,那么(i i-1),(i,i-2),(i,i-3)。。。。(i,j+1)这些连续子序列的值可以加上a的值;
(i,j),(i,j-1),(i,j-2),(i,1),这些值都会包含重复的i,j位置上的值,因为只需要算一次,所以不需要给这些以i
结尾的子序列加上a,这些子序列的个数,总共有j个,所以我们只需要用一个数组A标记上A[a]=i;那么d[i]=d[i-1]+a+(i-1-A[a])*a;
如果a之前没有出现过,那么A[a]等于0;如果a之前出现过,减去包含重复值的子序列的个数,也就是A[a]。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <set>
#include <algorithm>
#define LL long long
#define M 100100
using namespace std;
LL dp[M];
LL visit[M];
void init()
{
memset(dp,0,sizeof(dp));
memset(visit,0,sizeof(visit));
}
void solve()
{
int n;
LL data;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&data);
dp[i]=dp[i-1] + data +(i-1-visit[data]) * data;
visit[data]=i;
}
LL answer=0;
for(int i=1;i<=n;i++)
answer+=dp[i];
printf("%lld\n",answer);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
solve();
}
// system("pause");
return 0;
}

  

最新文章

  1. Chrome 桌面通知Notification
  2. HDFS 架构解析
  3. C#基础-文件夹复制与删除
  4. csharp:ASP.NET SignalR
  5. 【Swift】UITableViewCell 中 TTTAttributedLabel 超链接无法点击的问题
  6. 从MyEclipse转战到IntelliJ IDEA的经历
  7. 你会喜欢的25个创意的扁平化 LOGO 设计
  8. centos 修改DNS,网关,IP地址
  9. 深入理解Java:注解(Annotation)基本概念
  10. 出现Warning:Gradle version 2.10 is required. Current version is 2.8.
  11. Table of Contents - JavaSE
  12. spring jdbcTemplate源码剖析
  13. Function Currying in javascript 的一些注释
  14. 网络编程——TCP连接
  15. Boosting算法简介
  16. many bugs report when test bcm bt/wifi chip
  17. 编译APR包报错 rm: cannot remove `libtoolT&#39;: No such file or directory
  18. docker-2 tomcat
  19. ps遇到的技术问题列表
  20. Luogu 1093 - 奖学金 - [排序水题]

热门文章

  1. ABAP table control例子
  2. R语言数据管理(五)
  3. python3 计算文件夹中所有py文件里面代码行数,注释行数,空行数
  4. CVPR 2018:diractNets网络,有残差网络好吗?
  5. samsung n143 brightness on linux mint
  6. shell 比较运算符
  7. C#实现对外部程序的调用操作
  8. Spring源码解析-核心类之XmlBeanDefinitionReader
  9. linux 软件包的命名规则
  10. html5 + css3 + jQuery + 响应式布局设计