【BZOJ2212】[Poi2011]Tree Rotations 线段树合并

时间:2023-12-15 10:14:26

【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). 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并 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

题解:一开始以为跟蚯蚓那题一样,直接维护两个具有单调性的队列(或链表),然后合并一下就好了,结果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;
}