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 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.

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 题意就是给你一个数组,让你求1~1,1~2,1~3,1~4.... 1~n,
               2~2,2~3, 2~4.... 2~n
                 ....................
                      n-1~n-1 n-1~n
                           n~n
这些区间内不同数字的和,然后求总和。 思路:就是从右往左遍历,找某个数右边第一次出现这个数的位置,如果是第一次出现那么这个位置就是n
然后加加乘乘就行了 代码如下:
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e6+;
const int NN=1e5+;
int pos[N],a[NN];
int main()
{
int t;
int n;
scanf("%d",&t);
long long ans,tans;
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
pos[a[i]]=-;//标记每个数都没出现过
}
ans=,tans=;
for(int i=n-;i>=;i--)
{
if(pos[a[i]]==-)
tans+=a[i]*(n-i);
else
tans+=a[i]*(pos[a[i]]-i);
pos[a[i]]=i;
ans+=tans;
}
printf("%lld\n",ans);
}
return ;
}

Beauty of Array ZOJ - 3872(思维题)的更多相关文章

  1. ZOJ 3872: Beauty of Array(思维)

    Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ...

  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

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

  4. 2015 浙江省赛 Beauty of Array (思维题)

    Beauty of Array Edward has an array A with N integers. He defines the beauty of an array as the summ ...

  5. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...

  6. Beauty of Array(思维)

    Beauty of Array Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward has an array A with N integ ...

  7. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  8. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  9. ZOJ 3872 浙江2015年省赛试题

    D - Beauty of Array Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu S ...

随机推荐

  1. iOS UITextView 高度随文字自己主动添加,并尾随键盘移动(二)

    上节地址:http://blog.csdn.net/lwjok2007/article/details/47401293 接着上节我们来实现 输入框自己主动调节高度 首先,我们得知道,要推断是否该换行 ...

  2. 1732 Fibonacci数列 2

    1732 Fibonacci数列 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 在“1250 F ...

  3. extern_c

    (1)    使用extern和包含头文件来引用函数有什么区别呢?extern的引用方式比包含头文件要简洁得多!extern的使用方法是直截了当的,想引用哪个函数就用extern声明哪个函数.这大概是 ...

  4. [luoguP4142]洞穴遇险

    https://www.zybuluo.com/ysner/note/1240792 题面 戳我 解析 这种用来拼接的奇形怪状的东西,要不就是轮廓线\(DP\),要不就是网络流. 为了表示奇数点(即\ ...

  5. hadoop-Combiner作用用法

    文章来源http://blog.csdn.net/ipolaris/article/details/8723782 reduce的输入每个key所对应的value将是一大串1,但处理的文本很多时,这一 ...

  6. tinymce 富文本编辑器 编写资料

    tinymce官方文档: 粘贴图片插件 博客搬运地址 使用Blob获取图片并二进制显示实例页面 tinymce自动调整插件 是时候掌握一个富文本编辑器了——TinyMCE(1) XMLHttpRequ ...

  7. [App Store Connect帮助]二、 添加、编辑和删除用户(1)用户帐户和职能概述

    您可以在 App Store Connect 的“用户和访问”部分管理用户.添加沙盒测试员以及管理 API 密钥. 用户职能决定了用户对 App Store Connect 和 Apple Devel ...

  8. RocketMQ(1)--helloworld

    双Master方式: 服务器环境 序号 IP 角色 模式 1 192.168.32.135 nameServer1,brokerServer1  Master1 2 192.168.32.136 na ...

  9. Android 显示意图和隐式意图的区别

    意图在android的应用开发中是很重要的,明白了意图的作用和使用后,对开发会有很大帮助.如果没有把意图搞懂,以后开发应用会感觉缺些什么.        意图的作用:        1.激活组件   ...

  10. 355 Design Twitter 设计推特

    设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个功能:    postTweet(userI ...