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
n space-separated integers
a1, a2, ..., an
(1 ≤ ai ≤ 109).

Output

Print a single integer — the answer to the problem.

Sample test(s)
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

树状数组写起来更方便。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <stack>
#include <cctype>
#include <algorithm>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int mod = 99999997;
const int MAX = 0x3f3f3f3f;
const int maxn = 1000010;
int n, a, b;
int in[maxn], f[maxn], vis[maxn], l[maxn], r[maxn], tt[maxn];
int c[maxn];
int bs(int v, int x, int y) {
while(x < y) {
int m = (x+y) >> 1;
if(in[m] >= v) y = m;
else x = m+1;
}
return x;
}
int main()
{
cin >> n;
for(int i = 0; i < n; i++) {
scanf("%d", &in[i]);
tt[i] = in[i];
}
sort(in, in+n);
int m = unique(in, in+n) - in;
for(int i = 0; i < n; i++) {
f[i] = bs(tt[i], 0, m-1) + 1;
vis[ f[i] ]++;
l[i+1] = vis[ f[i] ];
}
memset(vis, 0, sizeof(vis));
for(int i = n-1; i >= 0; i--) {
f[i] = bs(tt[i], 0, m-1) + 1;
vis[ f[i] ]++;
r[i+1] = vis[ f[i] ];
}
LL sum = 0;
for(int i = 1; i <= n; i++) {
for(int j = r[i]+1; j <= maxn; j += j&-j) sum += c[j];
for(int j = l[i]; j > 0; j -= j&-j) c[j]++;
}
cout << sum << endl;
return 0;
}



最新文章

  1. Linux下的TeXlive 2015 中文问题
  2. Counting Bits(Difficulty: Medium)
  3. 10个优秀的JavaScript Web UI库/框架推荐
  4. mysql 外键(FOREIGN KEY)
  5. PeopleEditor允许客户端输入的同时验证输入的内容
  6. 图片原理解说(综合版:JPEG,PNG,BMP,GIF)
  7. [设计模式] 12 代理模式 proxy
  8. 网络上下载的Ghost系统含威胁
  9. Java开发中文件读取方式总结
  10. Mybatis学习笔记(三) 之Dao开发
  11. json格式数据整理
  12. webpack4.0各个击破(9)—— karma篇
  13. react state成员
  14. 让overflow:auto页面滚动条出现时不跳动
  15. Effective Java 第三版——69. 仅在发生异常的条件下使用异常
  16. Jena搭建SPARQL查询RDF数据
  17. ODAC(V9.5.15) 学习笔记(八)TOraScript
  18. 炫龙笔记本的gtx965m显卡玩游戏很卡
  19. 【下载】分享一个ida脚本,非常方便
  20. IIS中“绑定”,“IP地址全部未分配”到底是个什么玩意

热门文章

  1. 14.idea右键单击没有 svn选项处理办法
  2. Oracle 审计初步使用
  3. 学习《SQL必知必会(第4版)》中文PDF+英文PDF+代码++福达BenForta(作者)
  4. du---是对文件和目录磁盘使用的空间查看
  5. Python excel 功能扩展库 ——&gt; openpyxl 的基本使用
  6. 视图中使用ROWNUM要注意
  7. InnoDB引擎索引大观
  8. 利用css去除input按钮上的文字的几种方法
  9. 查看oracle数据库的启动时间
  10. C++里面virtual函数及虚表大小