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. POJ 3268 最短路应用

    POJ3268 题意很简单 正向图跑一遍SPFA 反向图再跑一边SPFA 找最大值即可. #include<iostream> #include<cstdio> #includ ...

  2. Python三次登陆

    题目:Python实现三次登陆 不要急于马上把三次登陆写出来,一定要将复杂的程序简单化,必须一步一步地去扩展,这样才保证不会出错. 步骤一:实现简单的一次登陆 # 事先定义 user = 'dark_ ...

  3. openstack instance resize Resize error: Unable to resize disk down

  4. T4模板使用记录,生成Model、Service、Repository

    自己目前在搭建一个.NET Core的框架,本来是打算使用前端做代码生成器直接生成到文件的,快做好了.感觉好像使用T4更方便一些,所以也就有了这篇文章~  我还是有个问题没解决,就是我想生成每个类(接 ...

  5. 关于BUG

    1.BUG的理解 2.提高BUG report的技巧

  6. [Codeforces]Codeforces Round #490 (Div. 3)

    Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...

  7. C#语言最基础的认识变量

    变量是指一块存储数据的内存空间,并且该内存区域的数据内容可以发生变化. 变量是必须先声明后赋值. 基本的语法:数据类型+变量名: 变量名=数据: 简单的使用技巧:声明赋值简写 Static void ...

  8. html5——应用缓存

    基本概念 1.HTML5中我们可以轻松的构建一个离线(无网络状态)应用,只需要创建一个cache manifest文件 2.可配置需要缓存的资源,网络无连接应用仍可用,本地读取缓存资源,提升访问速度, ...

  9. html5——过渡

    transition:1.开始状态   2.终止状态    3.过渡属性 transition: width 2s, background-color 2s;//属性,时间 transition: a ...

  10. jQuery——属相操作

    属性获取:attr(属性名), 属性设置:attr(属性名,具体值) 移除属性:removeAttr(属性名) 特殊情况:prop(属性名).prop(属性名,具体值):表单中状态属性checked. ...