2018.07.07 BZOJ2212: Poi2011Tree Rotations

2212: [Poi2011]Tree Rotations
Time Limit: 20 Sec Memory Limit: 259 MB
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

今天自学的新姿势:线段树合并,一个美(简)妙(单)的东西。这应该算是线段树合并的一道入门题目了。

那么我们先讲讲线段树合并是什么吧。^_^

相信各位大佬对常规形态的线段树都已经很了解了,也就是对于一些可以进行区间合并的运算利用以空间换时间的思想来将原本是 O n O 1 的询问和修改都平均成了 O l o g n 的。

但是!一般情况下不一定能这么做,因为并不是每一道题都允许我们可以高效的维护区间信息。怎么解决这个问题?线段树合并。

我们知道,当一颗线段树根节点的取值范围被确定之后,这棵树的最终形态也随之确定了。这样的话,对于两颗根节点取值范围相同的线段树,我们可以采用类似于左偏堆的启发式合并的方法合并这两颗线段树。合并的流程大概分个三步。

  1. a,b两颗线段树的当前节点有一个没有元素,直接返回非空的一个。
  2. a,b两颗线段树都访问到了叶节点,直接合并叶节点然后返回。
  3. 先分别将a,b两颗线段树的左右儿子合并,然后将两颗合并后的树连接起来。

从上述流程中可以看出,两颗线段树的合并的时间复杂度是基于它们的公共的节点数的。因此不难想到,当 1 > n 形成的线段树依次合并时,时间复杂度最高为 O n l o g n ,因此可以发现我们使用这种线段树合并的方法实际复杂度是有保障的,然后我们可以对比一下 t r e a p 的合并和线段树合并,平衡树的启发式合并的时间复杂度是 O n l o g n 2 的,而线段树合并却是 O n l o g n 的,这样看来,用线段树合并代替平衡树的启发式合并(如果题目允许的话)是可以起到给时间复杂度降阶的作用的。

s o h o w t o w r i t e t h e c o d e ?

合并部分的代码如下(这份代码与这道题有关):

inline int merge(int x,int y){
    if(!x||!y)return x+y;
    calc();
    son[x][0]=merge(son[x][0],son[y][0]);
    son[x][1]=merge(son[x][1],son[y][1]);
    pushup(x);
    return x;
}

其中 c a l c 函数在不同的题目下实现不同。

说了这么多,大家一定都有一个疑问,这线段树合并到底有什么用啊?

线段树合并的优点:

  • 所有操作都从上传到下,便于持久化。
  • 线段树合并的时间复杂度相对平衡树等复杂数据结构来说更优。
  • 代码实现简单,思考难度低。

但切记线段树合并并不是万能的方法。

回到这道题上吧,怎么做呢?

对于以 p 为根的子树,整颗子树中的逆序对数只与左子树中的逆序对数,右子树中的逆序对数,还有 ( x > y | x ϵ , y ϵ ) 的对数,由于前两者可以递归处理,且不会因子树位置的交换而改变,我们就只用考虑最后一项的变化了,最后一项无非就是从 ( x > y | x ϵ , y ϵ ) 的对数变成了 ( x > y | x ϵ , y ϵ ) 的对数,这个我们用权值线段树就可以处理了。

于是我们得到了时空复杂度均为 O n l o g n 的一个优秀的算法。当然空间复杂度可以更加优秀,直接压到 O n (由于本蒟蒻懒癌晚期没写)。

代码如下:

#include<bits/stdc++.h>
#define ll long long
#define N 400005
#define MN 4000005
using namespace std;
int n,a[N],s[N][2],root,rt[N],sum[MN],son[MN][2],tot=0,cnt=0;
ll ans0,ans1,ans;
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    return ans;
}
inline void build(int &p){
    p=++cnt;
    a[p]=read();
    if(a[p])return;
    build(s[p][0]);
    build(s[p][1]);
}
inline void pushup(int x){sum[x]=sum[son[x][0]]+sum[son[x][1]];}
inline void update(int &p,int l,int r,int v){
    if(!p)p=++tot;
    if(l==r){
        sum[p]=1;
        return;
    }
    int mid=(l+r)>>1;
    if(v<=mid)update(son[p][0],l,mid,v);
    else update(son[p][1],mid+1,r,v);
    pushup(p);
}
inline int merge(int x,int y){
    if(!x||!y)return x+y;
    ans0+=(ll)sum[son[x][1]]*(ll)sum[son[y][0]];
    ans1+=(ll)sum[son[x][0]]*(ll)sum[son[y][1]];
    son[x][0]=merge(son[x][0],son[y][0]);
    son[x][1]=merge(son[x][1],son[y][1]);
    pushup(x);
    return x;
}
inline void query(int p){
    if(a[p])return;
    query(s[p][0]),query(s[p][1]);
    ans0=ans1=0;
    rt[p]=merge(rt[s[p][0]],rt[s[p][1]]);
    ans+=min(ans0,ans1);
}
int main(){
    n=read();
    build(root);
    for(int i=1;i<=cnt;++i)
        if(a[i])update(rt[i],1,n,a[i]);
    query(root);
    printf("%lld",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/dreaming__ldx/article/details/80955523