【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\)个叶子节点,满足这些权值为 ...
随机推荐
- python-__init__.py 与模块对象的关系
python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文件,在另一个文件中需要import时,将事先写好的.py文件拷贝 到当前目录,或者是在sys.path中增加事先写好的. ...
- LaTeX 中使用三级标题
需要在导言区加入命令:\setcounter{secnumdepth}{4} 而后: \section{一级标题} \subsection{二级标题} \subsubsection{三级标题}
- IOT表
以前在接触索引的时候,就想过要是表字段太少,索引效果不是很不好吗,直接用索引不是更直接吗?后来因为懒惰也没有去查找相关资料.正好今天看到了table organization index,看了一下,实 ...
- Quartz.NET 实现定时任务调度
Quartz.NET Quick Start Guide Welcome to the Quick Start Guide for Quartz.NET. As you read this guide ...
- etymology-F
forsake [fə'seɪk] vt.放弃:断念. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400 ...
- Sql添加测试数据
--建测试表 CREATE TABLE T_UserInfo ( Userid varchar(20), UserName varchar(20), RegTime datetime, Tel va ...
- 常用 cdn
http://www.bootcdn.cn/ jquery <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.j ...
- atitit.薄伽梵歌overview attilax 读后感
atitit.薄伽梵歌overview attilax 读后感 1. 唯一一本记录神而不是神的代言人或者先知言论的经典 2 2. 篇章规模,字数 3 3. 内容摘要 3 4. 主要内容 3 4.1. ...
- Registers
https://github.com/JesusFreke/smali/wiki/Registers Introduction In dalvik's bytecode, registers are ...
- 李洪强和你一起学习前端之(3)Css基础和选择器
大家好! 经过了前面的学习,是不是对前端的学习有了初步的了解.虽然我之前有iOS开发的经验,现在接触一门新的语言,对我来说 有一定的优势,但是一门技术对于谁来说都是公平的,我承认,我在接触新知识的时候 ...