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;
}



CF #261 div2 D. Pashmak and Parmida&#39;s problem (树状数组版)的更多相关文章

  1. Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida&#39;s problem(求逆序数对)

    题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...

  2. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

  3. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)

    转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...

  4. codeforces 459D - Pashmak and Parmida&#39;s problem【离散化+处理+逆序对】

    题目:codeforces 459D - Pashmak and Parmida's problem 题意:给出n个数ai.然后定义f(l, r, x) 为ak = x,且l<=k<=r, ...

  5. Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组

    题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...

  6. Codeforces 216D Spider&#39;s Web 树状数组+模拟

    题目链接:http://codeforces.com/problemset/problem/216/D 题意: 对于一个梯形区域,假设梯形左边的点数!=梯形右边的点数,那么这个梯形为红色.否则为绿色, ...

  7. BestCoder Round #65 HDOJ5592 ZYB&#39;s Premutation(树状数组+二分)

    ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  8. codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)

    题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...

  9. CF459D Pashmak and Parmida's problem (树状数组)

    Codeforces Round #261 (Div. 2)   题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a ...

随机推荐

  1. 数据库中的Convert

    https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql Conversion failed ...

  2. CSS 文本字体颜色设置方法(CSS color)

    CSS 文本字体颜色设置方法(CSS color) 一.认识CSS 颜色(CSS color) 这里要介绍的是网页设置颜色包含有哪些:网页颜色规定规范. 1.常用颜色地方包含:字体颜色.超链接颜色.网 ...

  3. 20.发送http请求服务 ($http)

    转自:https://www.cnblogs.com/best/tag/Angular/ 服务从代码直接与服务器进行交互,底层是通过实现,与中http服务从AngularJS代码直接与Web服务器进行 ...

  4. COGS 577 蝗灾 线段树+CDQ分治

    第一次写cdq分治 感谢hhd&lty 这20亿对CP的指导(逃) 其实 就是 递归看左半部分对右半部分的贡献 (树状数组写挂了--临时改的线段树[大写的尴尬]) //By SiriusRen ...

  5. SQL Server 2005高可用性模式下创建数据库镜像

    SQL Server 2005高可用性模式下创建数据库镜像   高可用性模式下创建数据库镜像 第一步: --创建镜像用数据库-在主服务器上操作 create database db_mirror on ...

  6. 推荐的JavaScript编码规范

    http://www.qdfuns.com/notes/26812/7825414125719306fa409c709ee7b2a3.html

  7. Linux samba服务器安装调试。

    samba 做为主要的windown 和 Linux 通讯的服务器之一. 1. 查看是否安装了smb服务. #rpm -ga|grep samba     或者 #service smb status ...

  8. 统计学习:《贝叶斯思维统计建模的Python学习法》中文PDF+英文PDF+代码

    用数学工具解决实际问题仅有的要求可能就是懂一点概率知识和程序设计.而贝叶斯方法是一种常见的利用概率学知识去解决不确定性问题的数学方法,对于一个计算机专业的人士,应当熟悉其应用在诸如机器翻译,语音识别, ...

  9. CCF模拟题 最优灌溉

    最优灌溉 时间限制: 1.0s 内存限制: 256.0MB   问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很深的水井,所有的麦田都从这口井来引水灌溉. 为了灌溉,雷雷需 ...

  10. 将yyyyMMdd格式的字符串转成日期DateTime格式

    1.DateTime dt= DateTime.ParseExact("20110720", "yyyyMMdd", Thread.CurrentThread. ...