Pashmak and Parmida's problem

Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 ≤ rand 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 Input

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

这道题,虽然请教了暾暾和蒙蒙,但毕竟他们只是提供了思路,所以对于我自己居然手撸了线段树这件事情我还是比较满意的。感觉到自己的成长,fighting!!!
这个其实是一个比较简单的线段树问题。线段树的根节点rt保留介于l和r之间的相似的数的个数。
建树的过程:
rt=1,l=1,r=n; tree[rt]=x x表示从j到n,与aj相同的元素的个数。
在建树的过程中,查询从1到i,与a[i]相同的元素的个数k大于1到k-1的个数。
由于建树是从后向前查询。所以建树完成后,查询也刚好完成,并且得到结果。
#include<iostream>
#include<stdio.h>
#include<map>
#include<cstring>
using namespace std;
const int maxx = +;
map<int,int>mf;
map<int,int>mb;
int fro[maxx];
int bes[maxx];
int num[maxx];
int tree[maxx<<];
void add (int index,int l,int r,int rt)
{
tree[rt]++;
if(l==r) return;
int mid=(l+r)>>;
if(index<=mid)
add(index,l,mid,rt<<);
else
add(index,mid+,r,rt<<|);
}
int query(int inl,int inr,int l,int r,int rt)
{
//cout<<rt<<endl;
// printf("rt: %d l: %d r: %d **%d***%d\n",rt,l,r,inl,inr);
// char a;
//cin>>a;
if(inl>inr) return ;
int re=;
if(inr==r&&inl==l) return tree[rt];
int mid=(l+r)>>;
if(inr<=mid) re= query(inl,inr,l,mid,rt<<);
else if(inl>mid) re= query(inl,inr,mid+,r,rt<<|);
else if(inl<=mid&&inr>mid) {re=(query(inl,mid,l,mid,rt<<)+query(mid+,inr,mid+,r,rt<<|));}
//cout<<re<<endl;
return re;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
mf.clear();
mb.clear();
memset(tree,,sizeof(tree));
for(int i=;i<n;i++)
{
scanf("%d",&num[i]);
}
for(int i=;i<n;i++)
{
mf[num[i]]++;
fro[i]=mf[num[i]];
}
//cout<<fro[2]<<endl;
long long ans=;
for(int i=n-;i>;i--)
{
mb[num[i]]++;
add(mb[num[i]],,n,);
ans+=query(,fro[i-]-,,n,);
}
// for(int i=0;i<28+5;i++)
// cout<<tree[i]<<endl;
//cout<<"ss"<<endl;
// int ans=0;
//for(int i=0;i<n;i++)
// {
// }
printf("%I64d\n",ans);
}
}

最新文章

  1. ES6笔记(2)-- let的块级作用域
  2. 用css画出三角形【转】
  3. Volley 实现原理解析(转)
  4. python StringIO
  5. 20160730noip模拟赛zld
  6. iOS控件——UIView的viewWithTag:(int)findTag方法描述
  7. ASP.NET MVC 中使用JavaScriptResult
  8. ubuntu install express
  9. Linux中ssh的免密码登陆
  10. LVM逻辑卷管理@设备、格式、摩、引导自己主动安装一个完整的章节
  11. 批量删除的PHP
  12. R︱Softmax Regression建模 (MNIST 手写体识别和文档多分类应用)
  13. Player 播放器开源项目总结
  14. 【转】C# string数组转int数组
  15. Ubuntu 18.04 Server 设置静态IP
  16. iOS - Charles抓包数据
  17. Python3.5+SQL+Prometheus+Grafana报表/监控
  18. JS/JQuery获取当前元素的上一个/下一个兄弟级元素等元素的方法
  19. QFTPERROR lists
  20. stm32寄存器版学习笔记09 IIC

热门文章

  1. T-SQL经典语句(SQL server)
  2. C# DataGridView在单元格提示里(ToolTip)显示完整的单元格内容
  3. 转: SSH 公钥认证
  4. RS报表设计采用Total汇总过滤出错
  5. (转)Loader ,URLLoader ,URLStream的区别
  6. redis在linux下安装并測试(在spring下调用)
  7. eclipse解决editor does not contain a main type的方法
  8. HashMap源代码阅读
  9. vb 获取打印机名称
  10. taro 环境判断