【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
【BZOJ2212】[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
题解:一开始以为跟蚯蚓那题一样,直接维护两个具有单调性的队列(或链表),然后合并一下就好了,结果TLE
后来查题解发现要用线段树合并,顿时对时间复杂度产生了怀疑~
具体方法:如果想让总答案最优,那么肯定每个子树中的答案也得是最优的,所以我们只需要处理跨子树的最少逆序对个数,剩下的递归处理下去就好了
我们只需要再两棵线段树合并的时候顺便记录一下如果交换左右儿子,产生的逆序对个数以及不交换左右儿子,产生的逆序对个数,然后取最小值就行了
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn=400010;
typedef long long ll;
int n,root,tot,cnt;
ll ans,sum1,sum2;
int v[maxn],s[maxn*10],ls[maxn*10],rs[maxn*10],ch[maxn][2],rt[maxn];
void pushup(int x)
{
s[x]=s[ls[x]]+s[rs[x]];
}
int merge(int a,int b)
{
if(!b) return a;
if(!a) return b;
sum1+=(ll)s[ls[a]]*s[rs[b]],sum2+=(ll)s[ls[b]]*s[rs[a]];
ls[a]=merge(ls[a],ls[b]),rs[a]=merge(rs[a],rs[b]);
pushup(a);
return a;
}
void insert(int l,int r,int &x,int y)
{
if(!x) x=++tot;
if(l==r)
{
s[x]=1;
return ;
}
int mid=l+r>>1;
if(y<=mid) insert(l,mid,ls[x],y);
else insert(mid+1,r,rs[x],y);
pushup(x);
}
void dfs(int &x)
{
if(!x) x=++cnt;
scanf("%d",&v[x]);
if(v[x])
{
insert(1,n,rt[x],v[x]);
return ;
}
dfs(ch[x][0]),dfs(ch[x][1]);
sum1=sum2=0;
rt[x]=merge(rt[ch[x][0]],rt[ch[x][1]]);
ans+=min(sum1,sum2);
}
int main()
{
scanf("%d",&n);
dfs(root);
printf("%lld",ans);
return 0;
}
【BZOJ2212】[Poi2011]Tree Rotations 线段树合并的更多相关文章
- BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对
原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...
- bzoj2212[Poi2011]Tree Rotations [线段树合并]
题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...
- 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 ...
- BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...
- [bzoj2212]Tree Rotations(线段树合并)
解题关键:线段树合并模板题.线段树合并的题目一般都是权值线段树,因为结构相同,求逆序对时,遍历权值线段树的过程就是遍历所有mid的过程,所有能求出所有逆序对. #include<iostream ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- [POI2011]ROT-Tree Rotations 线段树合并|主席树 / 逆序对
题目[POI2011]ROT-Tree Rotations [Description] 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有\(n\)个叶子节点,满足这些权值为 ...
随机推荐
- mybatis加入条件
根据http://www.cnblogs.com/friends-wf/p/3799315.html搭建的环境 User.xml加入的 if where判断的 <!-- 根据条件查询一个用户 - ...
- Linux Load average负载详细解释
http://tianmaotalk.iteye.com/blog/1027970 Linux Load average负载详细解释 linux查看机器负载
- Git使用总结 Asp.net生命周期与Http协议 托管代码与非托管代码的区别 通过IEnumerable接口遍历数据 依赖注入与控制反转 C#多线程——优先级 AutoFac容器初步 C#特性详解 C#特性详解 WPF 可触摸移动的ScrollViewer控件 .NET(C#)能开发出什么样的APP?盘点那些通过Smobiler开发的移动应用
一,原理 首先,我们要明白Git是什么,它是一个管理工具或软件,用来管理什么的呢?当然是在软件开发过程中管理软件或者文件的不同版本的工具,一些作家也可以用这个管理自己创作的文本文件,由Linus开发的 ...
- python selenium --处理下拉框
下拉框是我们最常见的一种页面元素,对于一般的元素,我们只需要一次就定位,但下拉框里的内容需要进行两次定位,先定位到下拉框,再定位到下拉框内里的选项. drop_down.html <html&g ...
- 多线程-Thread,Runnable,Callable,Future,RunnableFuture,FutureTask
类图: 先看各自的源码: public interface Runnable { public abstract void run(); } public class Thread implement ...
- Atitit. http 代理原理 atiHttpProxy 大木马
Atitit. http 代理原理 atiHttpProxy 大木马 1. 面这张图可以清晰地阐明HttpProxy的实现原理:1 2. 代理服务器用途1 3. 其中流程具体如下:2 4. 设计规 ...
- Sublime Text 2/3如何支持中文GBK编码(亲测实现)
Sublime Text 2/3如何支持中文GBK编码 听语音 | 浏览:17594 | 更新:2014-03-17 10:52 1 2 3 4 5 分步阅读 Sublime Text默认是只支持UT ...
- List 通过 Collections.binarySearch 进行记录筛选
1. Collections.sort(list, new Comparator<TreeDto>() { @Override public int compare(TreeDto a2, ...
- django中使用POST方法报错 URL via POST, but the URL doesn't end in a slash
该方式是因为URL路径没有使用slash(斜线"/")结尾造成的. 因此在使用POST的JavaScript函数的路径参数中,路径URL必须使用/结尾.
- 解决eclipse偶尔无视breakpoint的行为
一般是如果你使用了T[]这样的参数列表,也就是generic array作为参数,你就算给函数打了断点,有时也会被eclipse无视 比如如下代码,你在调试main的时候,eclipse就会把doPa ...