码迷,mamicode.com
首页 > 其他好文 > 详细

[BZOJ2212][POI2011]Tree Rotations(线段树合并)

时间:2018-05-03 10:25:32      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:一点   权值线段树   lld   determine   problem   data   block   逆序对数   pre   

2212: [Poi2011]Tree Rotations

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 1562  Solved: 614
[Submit][Status][Discuss]

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

3
0
0
3
1
2

Sample Output

1

HINT

Source

[Submit][Status][Discuss]

线段树合并一般用在需要记录权值的时候,有时可以替代如树剖,LCT这样的数据结构。

线段树合并的好几道题BZOJ上都被权限了,所以就先列举几个应用:

 

1.[BZOJ2733]永无乡:一张图,要求支持 1)  连接u,v  2)  求某连通块的k大值。

做法:连通块用并查集维护,每个连通块建立一棵动态开点权值线段树,支持查询第k大。

2.[BZOJ4756][Usaco2017 Jan]Promotion Counting:一棵点权树,询问每个点在自己子树中的排名。

做法:同样对每个点建一棵权值线段树,从下往上合并即可。

3.[BZOJ3307]雨天的尾巴:一棵树,每次在u到v的路径上所有点上放一个物品w,最后输出所有点上放的最多的是哪个物品。

可以用树剖做。因为每次修改的是一个路径,所以做树上差分,同样从下往上合并得出每个点的权值线段树。

 

最后是这道题:发现每个子树的逆序对数=左子树的逆序对数+右子树的逆序对数+两个子树之间的逆序对数。

前两项分别递归下去,最后一项在合并左右两棵权值线段树的同时完成。

注意:

1.线段树合并的复杂度是两棵待合并树的重复节点个数,所以只有动态开点才能保证复杂度是$O(n\log n)$的。

2.(这一点只在BZOJ3307中体现了,因为另外几题都保证了权值互不相同)线段树合并的时候要对叶子单独处理,否则叶子节点的权值就会丢失。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define rep(i,l,r) for (int i=l; i<=r; i++)
 4 typedef long long ll;
 5 using namespace std;
 6  
 7 const int N=400100,M=4000100;
 8 int n,Rt,p,nd,tot,rt[N],a[N],sm[M],ls[M],rs[M],ch[M][2];
 9 ll ans,ans0,ans1;
10  
11 void ins(int &x,int L,int R,int pos){
12     if (!x) x=++nd;
13     if (L==R){ sm[x]=1; return; }
14     int mid=(L+R)>>1;
15     if (pos<=mid) ins(ls[x],L,mid,pos); else ins(rs[x],mid+1,R,pos);
16     sm[x]=sm[ls[x]]+sm[rs[x]];
17 }
18  
19 void Read(int &x){
20     x=++tot; scanf("%d",&a[x]);
21     if (!a[x]) Read(ch[x][0]),Read(ch[x][1]); else ins(rt[x],1,n,a[x]);
22 }
23  
24 int merge(int x,int y){
25     if (!x || !y) return x+y;
26     ans0+=1ll*sm[ls[x]]*sm[rs[y]];
27     ans1+=1ll*sm[rs[x]]*sm[ls[y]];
28     ls[x]=merge(ls[x],ls[y]); rs[x]=merge(rs[x],rs[y]);
29     sm[x]=sm[ls[x]]+sm[rs[x]]; return x;
30 }
31  
32 void solve(int x){
33     if (a[x]) return;
34     solve(ch[x][0]); solve(ch[x][1]);
35     ans0=ans1=0; rt[x]=merge(rt[ch[x][0]],rt[ch[x][1]]);
36     ans+=min(ans0,ans1);
37 }
38  
39 int main(){
40     scanf("%d",&n); Read(Rt); solve(Rt); printf("%lld\n",ans);
41     return 0;
42 }

 

[BZOJ2212][POI2011]Tree Rotations(线段树合并)

标签:一点   权值线段树   lld   determine   problem   data   block   逆序对数   pre   

原文地址:https://www.cnblogs.com/HocRiser/p/8983861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!