题目链接:

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.

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
题意:f[l,r,x]=在a[l],a[l+1]...a[r]中有多少个a[i]等于x,这道题可以问f[1,i,a[i]]>f[j,n,a[j]]&&1<=i<j<=n的i和j的对数,令pre[i]为a[i]在区间[1,i]中出现的次数,同理nex[j]为a[j]为区间[j,n]中a[j]出现的次数,结果就变成了求sigma{pre[i]和nex[i+1]的逆序对数}i为1到n-1;这时不是单单的一个数组求逆序对数(一个数组求逆序对数可以在归并排序中解决),所以得用线段树或者树状数组,树状数组还不会,等学会了树状数组再来更树状数组的代码;
AC代码:
/*~~~~~~~~线段树的代码~~~~~~~~~~~~~*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+;
int n,a[N],pre[N],nex[N];
struct nod
{
int l,r,sum;
};
nod tree[*N];
void build(int node,int le,int ri)
{
tree[node].l=le;
tree[node].r=ri;
tree[node].sum=;
if(le==ri)return ;
int mid=(le+ri)>>;
build(*node,le,mid);
build(*node+,mid+,ri);
tree[node].sum=tree[*node].sum+tree[*node+].sum;
}
int query(int node,int L,int R)
{
if(L<=tree[node].l&&R>=tree[node].r)
{
return tree[node].sum;
}
int mid=(tree[node].l+tree[node].r)>>;
if(R<=mid)return query(*node,L,R);
else if(L>mid)return query(*node+,L,R);
else return query(*node,L,R)+query(*node+,L,R);
}
int update(int node,int num)
{
if(tree[node].l==tree[node].r&&tree[node].l==num)
{
tree[node].sum+=;
return ;
}
int mid=(tree[node].l+tree[node].r)>>;
if(num<=mid)update(*node,num);
else update(*node+,num);
tree[node].sum=tree[*node].sum+tree[*node+].sum;
}
map<int,int>mp1,mp2;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
mp1[a[i]]++;
pre[i]=mp1[a[i]];//用map进行离散化
}
for(int i=n;i>;i--)
{
mp2[a[i]]++;
nex[i]=mp2[a[i]];
}
long long ans=;
build(,,n+);//建树时建到n+1,避免后面的nex[i]+1>n;
for(int i=;i<=n;i++)
{
if(nex[i]!=n)
ans+=(long long)query(,nex[i]+,n);
update(,pre[i]);
}
cout<<ans<<"\n";
return ;
}
/*~~~~~~~~树状数组的代码~~~~~~~~~~为什么用树状数组还没有线段树的快?*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+;
int n,a[N],pre[N],nex[N],sum[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x)
{
while(x<=n)
{
sum[x]++;
x+=lowbit(x);
}
}
int query(int x)
{
int s=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
map<int,int>mp1,mp2;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
mp1[a[i]]++;
pre[i]=mp1[a[i]];
}
for(int i=n;i>;i--)
{
mp2[a[i]]++;
nex[i]=mp2[a[i]];
}
long long ans=;
for(int i=n;i>;i--)
{
ans+=(long long)query(pre[i]-);
update(nex[i]);
}
cout<<ans<<"\n";
return ;
}

codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)的更多相关文章

  1. codeforces 540E 离散化技巧+线段树/树状数组求逆序对

    传送门:https://codeforces.com/contest/540/problem/E 题意: 有一段无限长的序列,有n次交换,每次将u位置的元素和v位置的元素交换,问n次交换后这个序列的逆 ...

  2. 【Codeforces 459D】Pashmak and Parmida's problem

    [链接] 我是链接,点我呀:) [题意] 定义两个函数 f和g f(i)表示a[1..i]中等于a[i]的数字的个数 g(i)表示a[i..n]中等于a[i]的数字的个数 让你求出来(i,j) 这里i ...

  3. 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, ...

  4. 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预处理出  ...

  5. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  6. CodeForces 459D Pashmak and Parmida's problem

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

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

  8. 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); 思路:先从左往右统 ...

  9. codeforces459D:Pashmak and Parmida's problem

    Description Parmida is a clever girl and she wants to participate in Olympiads this year. Of course ...

随机推荐

  1. Shell Error: -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory (转)

    错误原因可能有以下几种: 1.在WIN底下用文本编辑工具修改过参数变量,在保存的时候没注意编码格式造成的, 2.也有可能是在VIM里修改,第一行末尾按到ctrl_v 查看文件是DOS格式.UNIX格式 ...

  2. JSP的优势

    以下列出了使用JSP带来的其他好处: 与ASP相比:JSP有两大优势.首先,动态部分用Java编写,而不是VB或其他MS专用语言,所以更加强大与易用.第二点就是JSP易于移植到非MS平台上. 与纯 S ...

  3. Unity5 怎样做资源管理和增量更新

    工具 Unity 中的资源来源有三个途径:一个是Unity自己主动打包资源.一个是Resources.一个是AssetBundle. Unity自己主动打包资源是指在Unity场景中直接使用到的资源会 ...

  4. solr原理

    1.solr原理: 我本人的理解:solr是为解决高性能的全文索引而出现的,它将用户输入的关键字进行智能分解,分解成一个个词,过滤掉一些多余的停词及空格等,比如,“在”.“里面”.“也”.“的”.“它 ...

  5. Android 与H5之间的js交互

    之前项目做过一些Android和Html5之间js交互方面的东西,今天有时间就总结一下: 一.为什么要进行js交互: 为了方便原生开发和Html之间数据传递,在静态页面的情况下可以改变原生开发的页面: ...

  6. [Sdoi2014]数数[数位dp+AC自动机]

    3530: [Sdoi2014]数数 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 834  Solved: 434[Submit][Status][ ...

  7. 【BZOJ3112】[Zjoi2013]防守战线 单纯形法

    [BZOJ3112][Zjoi2013]防守战线 题解:依旧是转化成对偶问题,然后敲板子就行了~ 建完表后发现跟志愿者招募的表正好是相反的,感觉很神奇~ #include <cstdio> ...

  8. 九度OJ 1283:第一个只出现一次的字符 (计数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1808 解决:997 题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符 ...

  9. 20179209《Linux内核原理与分析》第十二周作

    缓冲区溢出漏洞实验 缓冲区溢出简介 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据缓冲器 ...

  10. BZOJXXXX: [IOI2000]邮局——四边形不等式优化初探

    貌似$BZOJ$上并没有这个题... 是嫌这个题水了么... 还是要氪金权限号??? 这里附上洛谷的题面:洛谷P4767 [IOI2000]邮局 题目描述 高速公路旁边有一些村庄.高速公路表示为整数轴 ...