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)" role="presentation" style="position: relative;">O(n)O(n)和O(1)" role="presentation" style="position: relative;">O(1)O(1)的询问和修改都平均成了O(logn)" role="presentation" style="position: relative;">O(logn)O(logn)的。

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

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

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

从上述流程中可以看出,两颗线段树的合并的时间复杂度是基于它们的公共的节点数的。因此不难想到,当1−&gt;n" role="presentation" style="position: relative;">1−>n1−>n形成的线段树依次合并时,时间复杂度最高为O(nlogn)" role="presentation" style="position: relative;">O(nlogn)O(nlogn),因此可以发现我们使用这种线段树合并的方法实际复杂度是有保障的,然后我们可以对比一下treap" role="presentation" style="position: relative;">treaptreap的合并和线段树合并,平衡树的启发式合并的时间复杂度是O(nlogn2)" role="presentation" style="position: relative;">O(nlogn2)O(nlogn2)的,而线段树合并却是O(nlogn)" role="presentation" style="position: relative;">O(nlogn)O(nlogn)的,这样看来,用线段树合并代替平衡树的启发式合并(如果题目允许的话)是可以起到给时间复杂度降阶的作用的。

so" role="presentation" style="position: relative;">soso how" role="presentation" style="position: relative;">howhow to" role="presentation" style="position: relative;">toto write" role="presentation" style="position: relative;">writewrite the" role="presentation" style="position: relative;">thethe code" role="presentation" style="position: relative;">codecode?

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

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;
}

其中calc" role="presentation" style="position: relative;">calccalc函数在不同的题目下实现不同。

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

线段树合并的优点:

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

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

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

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

于是我们得到了时空复杂度均为O(nlogn)" role="presentation" style="position: relative;">O(nlogn)O(nlogn)的一个优秀的算法。当然空间复杂度可以更加优秀,直接压到O(n)" role="presentation" style="position: relative;">O(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;
}

2018.07.07 BZOJ2212: Poi2011Tree Rotations(线段树合并)的更多相关文章

  1. [bzoj2212]Tree Rotations(线段树合并)

    解题关键:线段树合并模板题.线段树合并的题目一般都是权值线段树,因为结构相同,求逆序对时,遍历权值线段树的过程就是遍历所有mid的过程,所有能求出所有逆序对. #include<iostream ...

  2. bzoj2212 Tree Rotations 线段树合并+动态开点

    题目传送门 思路: 区间合并线段树的题,第一次写,对于一颗子树,无论这个子树怎么交换,都不会对其他子树的逆序对造成影响,所以就直接算逆序对就好. 注意叶子节点是1到n的全排列,所以每个权值都只会出现1 ...

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

    [BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...

  4. BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对

    原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...

  5. bzoj2212[Poi2011]Tree Rotations [线段树合并]

    题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...

  6. [POI2011]ROT-Tree Rotations 线段树合并|主席树 / 逆序对

    题目[POI2011]ROT-Tree Rotations [Description] 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有\(n\)个叶子节点,满足这些权值为 ...

  7. BZOJ.2212.[POI2011]Tree Rotations(线段树合并)

    题目链接 \(Description\) 给定一棵n个叶子的二叉树,每个叶节点有权值(1<=ai<=n).可以任意的交换两棵子树.问最后顺序遍历树得到的叶子权值序列中,最少的逆序对数是多少 ...

  8. NOI 2018 你的名字 (后缀自动机+线段树合并)

    题目大意:略 令$ION2017=S,ION2018=T$ 对$S$建$SAM$,每次都把$T$放进去跑,求出结尾是i的前缀串,能匹配上$S$的最长后缀长度为$f_{i}$ 由于$T$必须在$[l,r ...

  9. Bzoj P2212 [Poi2011]Tree Rotations | 线段树合并

    题目链接 通过观察与思考,我们可以发现,交换一个结点的两棵子树,只对这两棵子树内的节点的逆序对个数有影响,对这两棵子树以外的节点是没有影响的.嗯,然后呢?(っ•̀ω•́)っ 然后,我们就可以对于每一个 ...

  10. bzoj2212/3702 [Poi2011]Tree Rotations 线段树合并

    Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...

随机推荐

  1. JS检测当前设备是PC还是移动端

    用到的时候找到的分享下,也是收藏下,本地收藏多了感觉找起来很麻烦 方法1: function IsPC() { var userAgentInfo = navigator.userAgent; var ...

  2. EOF与子过程返回

        在2000及其以上系统,P处理语句GOTO新增了:EOF系统标签,意思是移动到当前P处理文件的结尾,EOF==END OF FILE的缩写,意为文件结尾,主要表现形式为:GOTO :EOFOR ...

  3. C#--反射技术

    反射:反射为了动态(比如动态的加载dll,动态创建类型.动态调用方法等) 引用 using System.Reflection 原理: 一个类库编译后会生成一个以.dll结尾的文件,一个以.pdb结尾 ...

  4. mysql insert on duplicate key, update, ignore

    insert 语句中不能使用where,所以如果需要根据插入的数据在已有的数据库表是否重复做一些操作可以使用下面三种方法: 1. 使用insert,捕获duplicate错误 2. insert in ...

  5. inline和inline-block的间隙问题

    我们在前端布局的时候,会偶尔发现,在具有inline/inline-block属性的元素间存在一小段间隙,网上有些文章说这个间隙是6px,但我觉得应该是一个空格的宽度. 这里以inline-block ...

  6. 行矩阵列矩阵D3D&GL&U3D

    void Start () { //矩阵函数原型:Matrix4x4(Vector4 colum0, Vector4 colum1, Vector4 colum2, Vector4 colum3),这 ...

  7. kafka相关资料

    先来说一下Kafka与RabbitMQ的对比: RabbitMQ,遵循AMQP协议,由内在高并发的erlanng语言开发,用在实时的对可靠性要求比较高的消息传递上. kafka是Linkedin于20 ...

  8. 科比投球预测-python实例

    链接:http://pan.baidu.com/s/1i4PNJlr 密码:fz7e import matplotlib.pyplot as pltfig = plt.figure()ax1 = fi ...

  9. r.js打包

    久闻r.js的大名,但实际没有用它做过任何东西.今天用它时,发现网上许多教程都不对.研究一下,把我的实际经验分享给大家. 例子1 先是HTML页面 <!DOCTYPE html> < ...

  10. %s %r 区别 转

    也可说是 str() 和 repr() 的区别 转自:http://blog.csdn.net/wusuopubupt/article/details/23678291 %r用rper()方法处理对象 ...