视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
CodeforcesRound#261(Div.2)D.PashmakandParmida&#3
2020-11-09 08:09:35 责编:小采
文档


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 con

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 denotef(l,?r,?x) the number of indicesk such that: l?≤?k?≤?r andak?=?x. His task is to calculate the number of pairs of indiciesi,?j (1?≤?i?j?≤?n) such thatf(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 containsn 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


由于原数过大,可使用map 映射,就不需要离散化了~两个数组,一个记录前面与本数相等的数的个数,一个记录后面与本数相等的数的个数~

题目便转化成求逆序数对,树状数组。答案long long

CODE:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
const int inf=0xfffffff;
typedef long long ll;
using namespace std;

const int Max=1000006;
int num[Max];
int f1[Max],f2[Max];
int c[Max];
mapm1,m2;
int n;

int lowbit(int x)
{
 return x&-x;
}
void add(int x,int d)
{
 while(x<=n){
 c[x]+=d;
 x+=lowbit(x);
 }
}
int sum(int x)
{
 int ans=0;
 while(x>0){
 ans+=c[x];
 x-=lowbit(x);
 }
 return ans;
}
int main()
{
 //freopen("in","r",stdin);
 while(~scanf("%d",&n)){
 m1.clear();
 m2.clear();
 memset(f1,0,sizeof(f1));
 memset(f2,0,sizeof(f2));
 memset(c,0,sizeof(c));
 for(int i=1;i<=n;i++){
 scanf("%d",&num[i]);
 }
 for(int i=1;i<=n;i++){
 f1[i]=(++m1[num[i]]);
 }
 for(int i=n;i>=1;i--){
 f2[i]=(++m2[num[i]]);
 }
 ll ans=0;
 for(int i=n-1;i>=1;i--){
 add(f2[i+1],1);
 ans+=(ll)sum(f1[i]-1);
 }
 printf("%Id\n",ans);
 }
 return 0;
}

下载本文
显示全文
专题