您的位置:首页 > 其它

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

2015-07-25 14:26 411 查看
题目传送门:http://codeforces.com/contest/459/problem/D

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


题意:给一个长度为n(n<1e6)的数组,并定义 f(l, r, x)为l到r这一段数组中等于x的个数。
求i,j的组数使得 f(1, i, ai) > f(j, n, aj)

题解:首先用Map,nlogn的方法求出所有的 f(1, i, ai) 和f(j, n, aj),

然后从左到右或者从右到左扫描一遍,用线段树或者树状数组记录每个数字出现的个数,然后求解即可。算是比较简单的数据结构题,没什么值得细讲的

#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
using namespace std;
int a[1000005];
int x;
int l[1000005];
int r[1000005];
long long ans;
map<int,int>m;
long long s[1000005*4];
void query(int l,int r,int o)
{
if (r<x) ans+=s[o];
if (r<x) return ;
if (l>x) return ;
if (l==r) return ;
int mid=(l+r)/2;
query(l,mid,2*o);
query(mid+1,r,2*o+1);
return ;
}
void add(int l,int r,int o)
{
if (l<=x&&x<=r) s[o]++;
if (r<x) return ;
if (l>x) return ;
if (l==r) return ;
int mid=(l+r)/2;
add(l,mid,2*o);
add(mid+1,r,2*o+1);
return ;
}
int main()
{
int n;
while (~scanf("%d",&n))
{
for (int i=1;i<=n;i++)scanf("%d",&a[i]);
m.clear();for (int i=1;i<=n;i++) {m[a[i]]++;l[i]=m[a[i]];}
m.clear();for (int i=n;i>=1;i--) {m[a[i]]++;r[i]=m[a[i]];}
memset(s,0,sizeof(s));
ans=0;
for (int i=n;i>=1;i--)
{
x=l[i];
query(1,n,1);
x=r[i];
add(1,n,1);
}
cout<<ans<<endl;

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: