Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem

来源:互联网 发布:手机录音播放软件 编辑:程序博客网 时间:2024/05/22 03:30

D. Pashmak and Parmida's problem
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 andak = 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
71 2 1 1 2 2 1
output
8
input
31 1 1
output
1
input
51 2 3 4 5
output
0      
开始没敢写,理了理思路才写,因为int wa了一次,然后改为long long ac,只能说cf数据真强,以及我估算数据的能力真弱,其实规模可以到(1e6)^2,超出int

大致思路,类比了求逆序数:
1、先离散化,因为数据范围达到了1e9

2、然后处理出ai[] aj[]数组,其含义注释,对ai[]建立树状数组处理,然后用aj[]倒着查询,最大是(1e6)^2 /2  所以要开long long的

#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <iostream>#include <iomanip>#include <cmath>#include <map>#include <set>#include <queue>using namespace std;#define ls(rt) rt*2#define rs(rt) rt*2+1#define ll long long#define ull unsigned long long#define rep(i,s,e) for(int i=s;i<e;i++)#define repe(i,s,e) for(int i=s;i<=e;i++)#define CL(a,b) memset(a,b,sizeof(a))#define IN(s) freopen(s,"r",stdin)#define OUT(s) freopen(s,"w",stdout)const ll ll_INF = ((ull)(-1))>>1;const double EPS = 1e-8;const int INF = 1e9+100;const int MAXN  = 1e6+100;inline int lowb(int x){return x&(-x);}ll c[MAXN];ll tmp[MAXN],a[MAXN],xcnt[MAXN],ycnt[MAXN];ll ai[MAXN],aj[MAXN];bool cmp(const int i, const int j){    return a[i]<a[j];}int n;void dis(){    sort(tmp+1,tmp+1+n,cmp);    int pre=a[tmp[1]]-1,st=0;    for(int i=1;i<=n;i++)    {        if(a[tmp[i]]!=pre)        {            pre=a[tmp[i]];            a[tmp[i]]=++st;        }        else            a[tmp[i]]=st;    }}void add(int x, int d){    while(x<=n)    {        c[x]+=d;        x+=lowb(x);    }}int sum(int x){    int ret=0;    while(x>0)    {        ret+=c[x];        x-=lowb(x);    }    return ret;}int main(){    //IN("D.txt");    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)            scanf("%I64d",&a[i]),tmp[i]=i;        dis();        CL(xcnt,0);CL(ycnt,0);       // int mmax=0;        for(int i=1;i<=n;i++)        {            xcnt[a[i]]++;            ai[i]=xcnt[a[i]]; // ai[i]  i之前有几个a[i]            //mmax=max(mmax,ai[i]);        }        CL(c,0);        ll ans=0;        /////////////////        //for(int i=1;i<=n;i++)         //   printf("a[%d]=%d ai=%d\n",i,a[i],ai[i]);        //////////////////        for(int i=n;i>1;i--)///        {            ycnt[a[i]]++;            aj[i]=ycnt[a[i]]; // aj[i]  i之后有介个a[i]            add(aj[i],1);            ans+=sum(ai[i-1]-1);        }        printf("%I64d\n",ans);    }    return 0;}





1 0
原创粉丝点击