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

Codeforces Round #261 (Div. 2) D的更多相关文章

  1. Codeforces Round #261 (Div. 2)[ABCDE]

    Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...

  2. Codeforces Round #261 (Div. 2) B

    链接:http://codeforces.com/contest/459/problem/B B. Pashmak and Flowers time limit per test 1 second m ...

  3. Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP

    http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35  36组数据 ...

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

  5. Codeforces Round #261 (Div. 2) - E (459E)

    题目连接:http://codeforces.com/contest/459/problem/E 题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度.(1≤n,m≤3 * ...

  6. Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题

    题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...

  7. Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

    题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 seco ...

  8. Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP

    题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...

  9. 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). 解法:分别从左到右,由右到 ...

  10. Codeforces Round #261 (Div. 2)

    第一场难得DIV2简单+AK人数多: E:给出一张图,求最多的边数,满足:在这个边的集合中后面的边的权值大于前面的边; 思路:我们将图按权值排列,以为只可能边权值小的跟新权值大的所以对于一条边我们只跟 ...

随机推荐

  1. Handler之IdleHandler

    MessageQueue提供了另一类消息,IdleHandler 如果返回false,每次轮询都会调用(理论上应该可以做一些别的东西) Looper.myQueue().addIdleHandler( ...

  2. 程序编写安全代码——sendto和recvfrom的大坑

    近日帮一个兄弟查代码问题,再处理完一系列问题以后,发现程序某些时候工作还是不正常,甚至会崩溃.因为环境所限,不能使用gdb,所以我只能review他的代码.最终发现原来是sendto和recvfrom ...

  3. Git使用之Permission Denied问题解决

    今天碰到了Git的Permission Denied问题. 在安装好git之后,我们通常会配置username和邮箱 git config --global user.name "zengj ...

  4. JAVA学习之 Model2中的Servlet与.NET一般处理程序傻傻分不清楚

    时隔多日,多日合适吗,应该是时隔多月.我又想起了一般处理程序.这都是由于近期在实现的DRP系统中经经常使用到jsp+servlet达到界面与逻辑的分离.servlet负责处理从jsp传回的信息:每当这 ...

  5. 实用API大全

    有道翻译API http://fanyi.youdao.com/openapi 有道翻译API支持中英互译,同时获得有道翻译结果和有道词典结果(可能没有),返回格式为XML或JSON.   百度翻译A ...

  6. strong and weak 强引用和弱引用的差别

    (weak和strong)不同的是 当一个对象不再有strong类型的指针指向它的时候 它会被释放  ,即使还有weak型指针指向它. 一旦最后一个strong型指针离去 .这个对象将被释放,全部剩余 ...

  7. GUI界面资源

    30个真棒的(免费iPhone,iPad IOS)的GUI 界面设计元素模板 做iphone开发的福音   创造力是指任何一种演示设计i的能力.无论是一个标志,旗帜,广告或一个完整的设计,能够注入的创 ...

  8. mysql优化-----多列索引的左前缀规则

    索引优化策略 :索引类型 .1B-tree索引 关注的是:Btree索引的左前缀匹配规则,索引在排序和分组上发挥的作用. 注:名叫btree索引,大的方面看都用的二叉树.平衡树.但具体的实现上,各引擎 ...

  9. 使用PHP对word文档进行操作的方法

    使用php时,因为加密等原因,如果直接用FILE后者OPEN等函数读取WORD的话往往是乱码,原来要使用COM 这是我简单的一个读取并存储到新的WORD上的文件<? // 建立一个指向新COM组 ...

  10. sublime text 3 安装vue 语法插件

    1.下载https://github.com/vuejs/vue-syntax-highlight,点击这里也可以下载压缩包 2.解压到C:\Users\***\AppData\Roaming\Sub ...