bzoj 2212 Tree Rotations
bzoj 2212 Tree Rotations
- 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内的逆序对数,跨越 \(ls,rs\) 的逆序对数三者之和.
- 交换 \(ls,rs\) 显然对前两种的答案没有影响,只需最大化最后一种答案.
- 对每个叶子节点开一棵权值线段树向上合并,选取权值中点 \(mid\) 划分开,那么两种情况在当前层产生的贡献即为 \(ls\) 的左子树大小 \(\times\) \(rs\) 的右子树大小,\(ls\) 的右子树大小 \(\times\) \(rs\) 的左子树大小中的最大值.
- 继续递归合并,就可以计算到所有贡献.
- 时间复杂度为 \(O(nlogn)\) .
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
const int MAXN=2e5+10;
ll ans=0,ans1,ans2;
struct node{
int siz,ls,rs;
}Tree[MAXN*30];
#define root Tree[o]
#define lson Tree[root.ls]
#define rson Tree[root.rs]
#define t(x) Tree[x]
int n,cnt=0;
void update(int &o,int l,int r,int pos)
{
if(!o)
o=++cnt;
++root.siz;
if(l==r)
return;
int mid=(l+r)>>1;
if(pos<=mid)
update(root.ls,l,mid,pos);
else
update(root.rs,mid+1,r,pos);
}
int merge(int ls,int rs)
{
if(!ls || !rs)
return ls+rs;
t(ls).siz+=t(rs).siz;
ans1+=1LL*(Tree[t(ls).ls].siz)*(Tree[t(rs).rs].siz);
ans2+=1LL*(Tree[t(ls).rs].siz)*(Tree[t(rs).ls].siz);
t(ls).ls=merge(t(ls).ls,t(rs).ls);
t(ls).rs=merge(t(ls).rs,t(rs).rs);
return ls;
}
int solve()
{
int p=read(),x=0;
if(!p)
{
int ls=solve();
int rs=solve();
ans1=ans2=0;
x=merge(ls,rs);
ans+=min(ans1,ans2);
return x;
}
else
update(x,1,n,p);
return x;
}
int main()
{
n=read();
solve();
cout<<ans<<endl;
return 0;
}
bzoj 2212 Tree Rotations的更多相关文章
- [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- 2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...
- BZOJ2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 391 Solved: 127[Submi ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- POI2011 Tree Rotations
POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...
- bzoj 2212: [Poi2011]Tree Rotations
Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...
- BZOJ 2212 [Poi2011]Tree Rotations(线段树合并)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2212 [题目大意] 给出一棵二叉树,每个叶节点上有一个权值,现在可以任意交换左右儿子, ...
- bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...
随机推荐
- 91. Decode Ways(动态规划 26个字母解码个数)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 587. Erect the Fence(凸包算法)
问题 给定一群树的坐标点,画个围栏把所有树围起来(凸包). 至少有一棵树,输入和输出没有顺序. Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] Output: ...
- 常用php操作redis命令整理(四)SET类型
SADD 将一个或多个member元素加入到集合key当中.(从左侧插入,最后插入的元素在0位置),集合中已经存在TK 则返回false,不存在添加成功 返回true <?php var_dum ...
- 搭建docker hadoop环境
目录 搭建Docker-Hadoop基础环境 简介 步骤 搭建Docker image ..待续 注释 搭建Docker-Hadoop基础环境 简介 因为很难真正的有一个集群环境.在一般的条件下想要模 ...
- Two Sum(II和IV)
本文包含leetcode上的Two Sum(Python实现).Two Sum II - Input array is sorted(Python实现).Two Sum IV - Input is a ...
- 彻底搞懂DOM事件处理(零)引子
通过合理使用JavaScript,可以为网站用户提供更好的交互体验.这主要是因为JavaScript能够让网站对用户的各种操作及时做出"反馈".这种"反馈"使网 ...
- Ubuntu 安装zookeeper
下载zookeeper Zookeeper下载 下载以后将文件迁移到/home/Hadoop/文件夹下面 hongdada@ubuntu:~/Downloads$ sudo mv zookeepe ...
- 在outlook中查找Skype的聊天记录
在outlook中和inbox平级,有一个Conversation History
- Docker:Err http://archive.ubuntu.com trusty InRelease & E: Unable to locate package [name] 问题
参考: Docker containers can't resolve DNS on Ubuntu 14.04 Desktop Host Unable to locate package错误解决办法 ...
- linux 系统忘记登录密码
linux6/6.5再启动时,按e ->在输入行最后面 输入空格 再输入single ->启动设置密码即可 单用户模式 在centos7需要 按e -> 然后滚动列表,找到ro(ro ...