codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)
题目链接:
D. Pashmak and Parmida's problem
3 seconds
256 megabytes
standard input
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.
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).
Print a single integer — the answer to the problem.
7
1 2 1 1 2 2 1
8
3
1 1 1
1
5
1 2 3 4 5
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(离散化+线段树或树状数组求逆序对)的更多相关文章
- codeforces 540E 离散化技巧+线段树/树状数组求逆序对
传送门:https://codeforces.com/contest/540/problem/E 题意: 有一段无限长的序列,有n次交换,每次将u位置的元素和v位置的元素交换,问n次交换后这个序列的逆 ...
- 【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 ...
- 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, ...
- 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预处理出 ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- CodeForces 459D Pashmak and Parmida's problem
Pashmak and Parmida's problem Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- 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 ...
- 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); 思路:先从左往右统 ...
- codeforces459D:Pashmak and Parmida's problem
Description Parmida is a clever girl and she wants to participate in Olympiads this year. Of course ...
随机推荐
- Python3 多线程 学习 threading
#-*- coding:utf-8 --*- #多线程测试 import time import datetime import threading def worker(): print(" ...
- Linux5_环境变量
1.总结背景 在linux系统下,下载并安装了应用程序,很有可能在键入它的名称时出现“command not found”的提示内容. 每次都到安装目标文件夹内,找到可执行文件来进行操作就太繁琐了.这 ...
- textarea中的内容的获取
今天他们说为啥获取不到textarea的数值 这个问题让我很纳闷 为什么会获取不到呢? 按照逻辑来说 同样都是表单元素 怎么可能出现呢? 我就看了一眼代码 alert($("#texta ...
- Java static关键字特点
一.特点 1.随着类的加载而加载2.优先于对象存在3.被类的所有对象共享4.可以通过类名调用 二.调用特征 静态方法: 成员变量:只能访问静态变量 成员方法:只能访问静态成员方法 非静态方法: 成员变 ...
- Swift开发教程--怎样设置状态栏的文字颜色
第一步:在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO 第二步:在viewDidLoad中加一句 UIApplication.sh ...
- images have the “stationarity” property, which implies that features that are useful in one region are also likely to be useful for other regions.
Convolutional networks may include local or global pooling layers[clarification needed], which combi ...
- phpstorm+xdebug, 实现断点调试: xdebug如何配置
[XDebug] xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug" xdebug.trace_output_dir=" ...
- PHP环境变量归纳(转自网络)
PHP环境变量主要有$GLOBALS[].$_SERVER[].$_GET[].$_POST[].$_COOKIE[].$_FILES[].$_ENV[].$_REQUEST[].$_SESSION[ ...
- pinpoint改造支持查询
原架构 改造后架构
- FOXMAIL提示容量满无法收邮件,清除旧邮件后还是无法收取,请问如何解决?
FOXMAIL提示容量满无法收邮件,清除旧邮件后还是无法收取,请问如何解决? 2009-03-23 11:21包子燕 分类:网站使用 我清除了FOXMAIL所在的磁盘空间,共有12G,也删除了部分旧 ...