Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem(树状数组+逆序数对)

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 <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
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];
map<int,int>m1,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("%I64d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值