BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
Description
Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some interesting features: The tree consists of straight branches, bifurcations and leaves. The trunk stemming from the ground is also a branch. Each branch ends with either a bifurcation or a leaf on its top end. Exactly two branches fork out from a bifurcation at the end of a branch - the left branch and the right branch. Each leaf of the tree is labelled with an integer from the range . The labels of leaves are unique. With some gardening work, a so called rotation can be performed on any bifurcation, swapping the left and right branches that fork out of it. The corona of the tree is the sequence of integers obtained by reading the leaves' labels from left to right. Byteasar is from the old town of Byteburg and, like all true Byteburgers, praises neatness and order. He wonders how neat can his tree become thanks to appropriate rotations. The neatness of a tree is measured by the number of inversions in its corona, i.e. the number of pairs(I,j), (1< = I < j < = N ) such that(Ai>Aj) in the corona(A1,A2,A3…An).
The original tree (on the left) with corona(3,1,2) has two inversions. A single rotation gives a tree (on the right) with corona(1,3,2), which has only one inversion. Each of these two trees has 5 branches. Write a program that determines the minimum number of inversions in the corona of Byteasar's tree that can be obtained by rotations.
现在有一棵二叉树,所有非叶子节点都有两个孩子。在每个叶子节点上有一个权值(有n个叶子节点,满足这些权值为1..n的一个排列)。可以任意交换每个非叶子节点的左右孩子。
要求进行一系列交换,使得最终所有叶子节点的权值按照遍历序写出来,逆序对个数最少。
Input
In the first line of
the standard input there is a single integer (2< = N < = 200000)
that denotes the number of leaves in Byteasar's tree. Next, the
description of the tree follows. The tree is defined recursively: if
there is a leaf labelled with ()(1<=P<=N) at the end of the trunk
(i.e., the branch from which the tree stems), then the tree's
description consists of a single line containing a single integer , if
there is a bifurcation at the end of the trunk, then the tree's
description consists of three parts: the first line holds a single
number , then the description of the left subtree follows (as if the
left branch forking out of the bifurcation was its trunk), and finally
the description of the right subtree follows (as if the right branch
forking out of the bifurcation was its trunk).
第一行n
下面每行,一个数x
如果x==0,表示这个节点非叶子节点,递归地向下读入其左孩子和右孩子的信息,
如果x!=0,表示这个节点是叶子节点,权值为x
1<=n<=200000
Output
In the first and
only line of the standard output a single integer is to be printed: the
minimum number of inversions in the corona of the input tree that can be
obtained by a sequence of rotations.
一行,最少逆序对个数
Sample Input
0
0
3
1
2
Sample Output
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 400050
#define maxn 100000000
typedef long long ll;
int lson[N],rson[N],root[N],ls[N*40],rs[N*40],t[N*40],n,cnt,tot;
ll ans,tmp1,tmp2;
void update(int l,int r,int x,int &p) {
if(!p) p=++tot;
if(l==r) {
t[p]=1;
return ;
}
int mid=(l+r)>>1;
if(x<=mid) update(l,mid,x,ls[p]);
else update(mid+1,r,x,rs[p]);
t[p]=t[ls[p]]+t[rs[p]];
}
void build(int &p) {
int x;
p=++cnt;
scanf("%d",&x);
if(x) {
update(1,maxn,x,root[p]);
}else {
build(lson[p]); build(rson[p]);
}
}
int merge(int x,int y) {
if(!x) return y;
if(!y) return x;
tmp1+=1ll*t[ls[x]]*t[rs[y]];
tmp2+=1ll*t[rs[x]]*t[ls[y]];
ls[x]=merge(ls[x],ls[y]);
rs[x]=merge(rs[x],rs[y]);
t[x]=t[ls[x]]+t[rs[x]];
return x;
}
void dfs(int x) {
if(!lson[x]) return ;
dfs(lson[x]); dfs(rson[x]);
tmp1=0;tmp2=0;
root[x]=merge(root[lson[x]],root[rson[x]]);
ans+=min(tmp1,tmp2);
}
int main() {
scanf("%d",&n);
int tmp=1;
build(tmp);
dfs(1);
printf("%lld\n",ans);
}
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并的更多相关文章
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- bzoj2212[Poi2011]Tree Rotations [线段树合并]
题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...
- BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对
原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...
- BZOJ.2212.[POI2011]Tree Rotations(线段树合并)
题目链接 \(Description\) 给定一棵n个叶子的二叉树,每个叶节点有权值(1<=ai<=n).可以任意的交换两棵子树.问最后顺序遍历树得到的叶子权值序列中,最少的逆序对数是多少 ...
- Bzoj P2212 [Poi2011]Tree Rotations | 线段树合并
题目链接 通过观察与思考,我们可以发现,交换一个结点的两棵子树,只对这两棵子树内的节点的逆序对个数有影响,对这两棵子树以外的节点是没有影响的.嗯,然后呢?(っ•̀ω•́)っ 然后,我们就可以对于每一个 ...
- bzoj2212/3702 [Poi2011]Tree Rotations 线段树合并
Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...
- BZOJ2212【POI2011】ROT:Tree Rotation 线段树合并
题意: 给一棵n(1≤n≤200000个叶子的二叉树,可以交换每个点的左右子树,要求叶子遍历序的逆序对最少. 分析: 求逆序对我们可以想到权值线段树,所以我们对每个点建一颗线段树(为了避免空间爆炸,采 ...
- [POI2011]ROT-Tree Rotations 线段树合并|主席树 / 逆序对
题目[POI2011]ROT-Tree Rotations [Description] 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有\(n\)个叶子节点,满足这些权值为 ...
- 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]
题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...
随机推荐
- Idea(一) 安装与破解
现在idea横行的时代,没用过idea都不好意思了,于是乎,我也下载感受下. 下载安装包和破解地址: 链接: https://pan.baidu.com/s/16OeiDw942JaPXKtc9Oz1 ...
- oracle索引建立和删除
1.多列建立索引 SQL> create index dex_index2 on dex(sex,name); Index created. SQL> select object_name ...
- win8 JDK环境变量不生效
执行where java 看一下路径对不对,如果对的话就把system32下面的3个java相关的exe删了即可,如果路径不对就修改环境变量.
- Spring Aop中,获取被代理类的工具
在实际应用中,顺着过去就是一个类被代理.反过来,可能需要逆向进行,拿到被代理的类,实际工作中碰到了,就拿出来分享下. /** * 获取被代理类的Object * @author Monkey */ p ...
- Day15 Javascipt内容补充
JavaScript函数: 函数: function 函数名(a,b,c){ 执行代码 } 1,如何去找到标签 Dom直接选择器: 1,找到标签 #获取单个元素 document.getElement ...
- 一篇文章带你了解Cloud Native
背景 Cloud Native表面看起来比较容易理解,但是细思好像又有些模糊不清:Cloud Native和Cloud关系是啥?它用来解决什么问题?它是一个新技术还是一个新的方法?什么样的APP符合“ ...
- Android hybrid App项目构建和部分基本开发问题
1.首先是选型:Cordova+Ionic Framework,调试测试环境是Ripple Emulator.开发环境其实可以随便选,我个人选择了Eclipse,当然Android SDK+ADT也是 ...
- 最优Django环境配置
2 最优Django环境配置 本章描述了我们认为对于中等和高级Django使用者来说最优的本地环境配置 2.1 统一使用相同的数据库引擎 一个常见的开发者错误是在本地开发环境中使用SQLite3,而在 ...
- JS中的top是什么?
<iframe/>或者<frame>里面用主页面的东西,就是top.xxx如:<script> function func(){ ... };</script ...
- java中面试可能会问的问题
为了明年的面试,把面试中可能遇到的关于java的问题记录在下面,纯个人理解,如果有误,请指正! 1.java中拷贝的三种方式,以及他们的区别. 这三种方式分别是:直接赋值,浅拷贝,深拷贝.第一种直接赋 ...