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 ≤ r and 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.

Examples
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

正解:离散化+树状数组
解题报告:
  首先离散化之后,可以预处理一下每个元素的前驱相等元素个数和后继相等元素个数,O(NlogN)
  之后我们可以得到每个元素的两个值,前驱个数值pre[i]和后继个数值next[i],我们的任务就是查找i和j满足pre[i]>next[j] && i<j的个数。
  感觉是不是很像逆序对?直接从后往前往树状数组中插入next值(记得上界是n,处理一下0),每次查找比当前pre小的next个数,因为我们是从后往前插入的next,所以可以保证i<j
  codeforces的评测机真快,100w数据0.4S,丝毫不虚
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int MAXN = ;
int n,a[MAXN],u[MAXN];
int pre[MAXN],next[MAXN];
int jump1[MAXN],jump2[MAXN];
bool vis[MAXN];
int shu[MAXN],L;//树状数组维护共有多少个小于他的数
LL ans;
struct node{
int val,id;
}b[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline bool cmp(node q,node qq){ if(q.val==qq.val) return q.id<qq.id; return q.val<qq.val; } inline void add(int x,int val){
while(x<=n+) {
shu[x]+=val;
x+=x&(-x);
}
} inline int query(int x){
int total=;
while(x) {
total+=shu[x];
x-=x&(-x);
}
return total;
} inline void work(){
n=getint(); for(int i=;i<=n;i++) u[i]=b[i].val=a[i]=getint(),b[i].id=i,jump2[i]=n+;
sort(b+,b+n+,cmp);
for(int i=;i<=n;i++) if(b[i].val==b[i-].val) jump1[b[i].id]=b[i-].id,jump2[b[i-].id]=b[i].id;
sort(u+,u+n+);
L=unique(u+,u+n+)-u-;
for(int i=;i<=n;i++) a[i]=lower_bound(u+,u+L+,a[i])-u;
int x;
for(int i=;i<=n;i++) {
if(vis[i]) continue; vis[i]=;
x=i; while(x<=n) x=jump2[x],pre[x]=pre[jump1[x]]+,vis[x]=; }
memset(vis,,sizeof(vis));
for(int i=n;i>=;i--) {
if(vis[i]) continue; vis[i]=;
x=i; while(x) x=jump1[x],next[x]=next[jump2[x]]+,vis[x]=;
} for(int i=n;i;i--) {
ans+=query(pre[i]);
add(next[i]+,);
}
printf("%lld",ans);
} int main()
{
work();
return ;
}

 

codeforces459D:Pashmak and Parmida's problem的更多相关文章

  1. CodeForces 459D Pashmak and Parmida's problem

    Pashmak and Parmida's problem Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  2. codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)

    题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...

  3. cf459D Pashmak and Parmida's problem

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

  4. codeforces D. Pashmak and Parmida's problem

    http://codeforces.com/contest/459/problem/D 题意:给你n个数,然后统计多少组(i,j)使得f(1,i,ai)>f(j,n,aj); 思路:先从左往右统 ...

  5. codeforces 459 D. Pashmak and Parmida's problem(思维+线段树)

    题目链接:http://codeforces.com/contest/459/problem/D 题意:给出数组a,定义f(l,r,x)为a[]的下标l到r之间,等于x的元素数.i和j符合f(1,i, ...

  6. Pashmak and Parmida's problem(树状数组)

    题目链接:http://codeforces.com/contest/459/problem/D 题意: 数列A, ai表示 i-th 的值, f(i,j, x) 表示[i,j]之间x的数目, 问:当 ...

  7. CF459D Pashmak and Parmida's problem (树状数组)

    Codeforces Round #261 (Div. 2)   题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a ...

  8. Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组

    题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...

  9. Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

随机推荐

  1. SqlProfiler的替代品-ExpressProfiler

    可以用来跟踪执行的sql语句.安装SqlServer之后SqlServerManagementStudio自带一个SqlProfiler,但是如果安装的SqlExpress,那就没有了. 项目的主页在 ...

  2. Linux python3安装/shell脚本/if/循环/函数

    python3安装 安装过程 安装包: wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgztar -xvf Python-3.7 ...

  3. Django中_Meta 部分用法

    周一了,就不长篇大论了,给大家分享一个很实用的知识点,希望大家周末过得开心,愉快,诗和远方在等着你们.而我还在苦逼的撸代码,只为了应付眼前的苟且! model.UserInfo._meta.app_l ...

  4. hdu 2112 HDU Today(map与dijkstra的结合使用)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. github 上 机器学习 的库推荐列表

    awesome-machine-learning: https://github.com/josephmisiti/awesome-machine-learning

  6. 类百度DOC编辑区域

    .mainarea{ position:absolute; top:151px; width:100%; bottom:0px; } .edit_wrap{ background:#fcfcfc; p ...

  7. 常见Web源码泄露总结

    来自:http://www.hacksec.cn/Penetration-test/474.html 摘要 背景 本文主要是记录一下常见的源码泄漏问题,这些经常在web渗透测试以及CTF中出现. .h ...

  8. Loadrunder常见问题汇总(持续更新)

    1.LR 脚本为空的解决方法: 1)如果安装了IE以外的浏览器,并且IE不是默认浏览器,则无法生成录制脚本 2)如果录制脚本时IE不能打开,则需要将浏览器的IE工具高级选项中,将“启用第三方浏览器扩展 ...

  9. mysql安装配置 (单个mysql安装)

    mysql安装于 c盘因为有注册文件 和注册表 1.为了防止mysql数据表变大和sql日志增加   改变C:\ProgramData\MySQL\MySQL Server 5.7\my.ini 得指 ...

  10. Linux centos7 安装 keepalived-2.0.6

    1.下载(版本:2.0.6) cd /home/install/ wget http://124.205.69.170/files/1255000006EF2AA1/www.keepalived.or ...