Description

Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.

There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).

Help Pashmak with the test.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains nspace-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print a single integer — the answer to the problem.

Examples
input
7
1 2 1 1 2 2 1
output
8
input
3
1 1 1
output
1
input
5
1 2 3 4 5
output
0
题意:定义f(l, r, x),是l~r中等于x的有多少个,现在问f(1, i, ai) > f(j, n, aj)中i,j有多少种
解法:预处理f(1, i, ai) ,f(j, n, aj)的个数,然后用树状数组维护
比如最大值为3,则维护到3,如下插入第一个元素
1 0 0 ,我们要求大于2的个数,只需要查询(2-1=1)即(1~1)区间的和就行
即:查询大于x的个数,求(1~x-1)区间的个数
 #include<bits/stdc++.h>
using namespace std;
int ans[];
int ll[],rr[];
int bit(int x)
{
return x&(-x);
}
int add(int adr,int num,int lim)
{
// cout<<adr<<" "<<num<<" "<<lim<<endl;
while(adr<=lim)
{
ans[adr]+=num;
adr+=bit(adr);
}
}
int sum(int lim)
{
int s=;
while(lim)
{
s+=ans[lim];
lim-=bit(lim);
}
return s;
}
int m[];
int main()
{ long long num=;
map<int,int>q,p;
int n;
int Max=-;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&m[i]);
// Max=max(m[i],Max);
q[m[i]]++;
Max=max(q[m[i]],Max);
ll[i]=q[m[i]];
}
for(int i=n-;i>=;i--)
{
p[m[i]]++;
rr[i]=p[m[i]];
}
//cout<<"A"<<endl;
for(int i=n-;i>;i--)
{
// cout<<rr[i]<<endl;
add(rr[i],,Max);
// cout<<i<<endl;
num+=sum(ll[i-]-);
}
printf("%lld\n",num);
return ;
}
 

最新文章

  1. C#设计模式系列:开闭原则(Open Close Principle)
  2. poj 3744 Scout YYF I(概率dp,矩阵优化)
  3. mfc_Demo
  4. exit(0)和exit(1)区别
  5. 修改linux默认jdk版本
  6. oracle 常用技巧及脚本
  7. firefox必备扩展
  8. spring之注解
  9. 文科生细谈学习Linux系统的重要性
  10. UVa11212,Editing a Book
  11. 用python爬取微博数据并生成词云
  12. pt-show-grants
  13. 史上最全java面试题
  14. &#39;An instance 0x155e74a0 of class UIWebView was deallocated while key value observers were still registered with it.
  15. Confluence 6 在你用户宏中使用参数
  16. 二: drf视图
  17. Python code 提取UML
  18. windows10中远程登录ubuntu16.04的桌面
  19. VC++深入详解-第四章学习心得
  20. syslinux启动盘制作

热门文章

  1. C#高阶与初心:(二)P/Invoke平台调用
  2. &amp;lt;十二&amp;gt;读&amp;lt;&amp;lt;大话设计模式&amp;gt;&amp;gt;之状态模式
  3. A toolbox to build your own build server
  4. 无节操cocos2d-js游戏
  5. Linux系统(Centos)下安装Java环境配置步骤详述
  6. QT实现FTP服务器(三)
  7. Fibonacci数列(找规律)
  8. timestamp 转 date 处理后再转timestamp
  9. adb获取Android系统属性(adb shell getprop ***)数据来源
  10. 一步一步学Silverlight 2系列(26):基本图形