原文地址:http://www.cnblogs.com/GXZlegend/p/6826614.html


题目描述

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的一个排列)。可以任意交换每个非叶子节点的左右孩子。
要求进行一系列交换,使得最终所有叶子节点的权值按照遍历序写出来,逆序对个数最少。

输入

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

输出

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.

一行,最少逆序对个数

样例输入

3
0
0
3
1
2

样例输出

1


题解

权值线段树合并

根节点的最小逆序对数=子树的最小逆序对数+左右子树间的最小逆序对数

由于子树内不影响子树间,所以按照相同的策略递归进行即可。

一开始写了queue,然而MLE,同学写了链表结果TLE。

于是按照网上大犇的做法写了权值线段树合并。

将x与y合并,即将ls[x]与ls[y]合并,将rs[x]与rs[y]合并,这同时可以统计逆序对个数。

然后两种方法取最小值即可。

总合并的时间复杂度为是$O(n\log n)$。

#include <cstdio>
#include <cstring>
#include <queue>
#define lson l , mid , ls[x]
#define rson mid + 1 , r , rs[x]
#define N 400010
using namespace std;
typedef long long ll;
int n , lp[N] , rp[N] , fa[N] , tot = 1 , root[N] , ls[N * 10] , rs[N * 10] , si[N * 10] , cnt;
ll ans , tmp1 , tmp2;
void pushup(int x)
{
si[x] = si[ls[x]] + si[rs[x]];
}
void ins(int p , int l , int r , int &x)
{
if(!x) x = ++cnt;
if(l == r)
{
si[x] = 1;
return;
}
int mid = (l + r) >> 1;
if(p <= mid) ins(p , lson);
else ins(p , rson);
pushup(x);
}
void build()
{
int now = tot , t;
scanf("%d" , &t);
if(t) ins(t , 1 , n , root[now]);
else lp[now] = ++tot , build() , rp[now] = ++tot , build();
}
int merge(int x , int y)
{
if(!x) return y;
if(!y) return x;
tmp1 += (ll)si[ls[x]] * si[rs[y]] , tmp2 += (ll)si[rs[x]] * si[ls[y]];
ls[x] = merge(ls[x] , ls[y]) , rs[x] = merge(rs[x] , rs[y]);
pushup(x);
return x;
}
void dfs(int x)
{
if(!lp[x]) return;
dfs(lp[x]) , dfs(rp[x]);
tmp1 = tmp2 = 0;
root[x] = merge(root[lp[x]] , root[rp[x]]);
ans += min(tmp1 , tmp2);
}
int main()
{
scanf("%d" , &n);
build();
dfs(1);
printf("%lld\n" , ans);
return 0;
}

【bzoj2212】[Poi2011]Tree Rotations 权值线段树合并的更多相关文章

  1. 【bzoj1977】[BeiJing2010组队]次小生成树 Tree 最小生成树+权值线段树合并

    题目描述 求一张图的严格次小生成树的边权和,保证存在. 输入 第一行包含两个整数N 和M,表示无向图的点数与边数. 接下来 M行,每行 3个数x y z 表示,点 x 和点y之间有一条边,边的权值为z ...

  2. B20J_2733_[HNOI2012]永无乡_权值线段树合并

    B20J_2733_[HNOI2012]永无乡_权值线段树合并 Description:n座岛,编号从1到n,每座岛都有自己的独一无二的重要度,按照重要度可以将这n座岛排名,名次用1到 n来表示.某些 ...

  3. 【bzoj4719】[Noip2016]天天爱跑步 权值线段树合并

    题目描述 给出一棵n个点的树,以及m次操作,每次操作从起点向终点以每秒一条边的速度移动(初始时刻为0),最后对于每个点询问有多少次操作在经过该点的时刻为某值. 输入 第一行有两个整数N和M .其中N代 ...

  4. luogu3224 永无乡(动态开点,权值线段树合并)

    luogu3224 永无乡(动态开点,权值线段树合并) 永无乡包含 n 座岛,编号从 1 到 n ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 n 座岛排名,名次用 1 到 n 来表示.某些 ...

  5. 【bzoj4399】魔法少女LJJ 并查集+权值线段树合并

    题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...

  6. 【bzoj3307】雨天的尾巴 权值线段树合并

    题目描述 N个点,形成一个树状结构.有M次发放,每次选择两个点x,y,对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入 第一行数字N,M接下来 ...

  7. HDU-6704 K-th occurrence (后缀自动机father树上倍增建权值线段树合并)

    layout: post title: HDU-6704 K-th occurrence (后缀自动机father树上倍增建权值线段树合并) author: "luowentaoaa&quo ...

  8. BZOJ2733/LG3324 「HNOI2014」永无乡 权值线段树合并

    问题描述 BZOJ2733 LG3224 题解 对于每个结点建立一棵权值线段树. 查询操作就去查询第 \(k\) 大,合并操作就合并两颗权值线段树. 并查集维护连通性. 同时 STO hkk,zcr, ...

  9. bzoj3307雨天的尾巴(权值线段树合并/DSU on tree)

    题目大意: 一颗树,想要在树链上添加同一物品,问最后每个点上哪个物品最多. 解题思路: 1.线段树合并 假如说物品数量少到可以暴力添加,且树点极少,我们怎么做. 首先在一个树节点上标记出哪些物品有多少 ...

随机推荐

  1. EBS R12中FND凭证打印警告:OPP响应超时

    接近年关,最近年结忙的飞起,此为背景,今天运维那边反应日记账凭证打印报错,看了下后台请求发现请求有警告. 查看日志发现报了“并发:OPP响应超时”的警告,这个地方响应超时可能是配置文件中“并发:OPP ...

  2. util.Date与sql.Date转换

    一. 时间类型 1.  sql包下, Date:只有年月日. Time:只有时分秒. Timestamp:表示时间戳,有年月日时分秒,以及毫秒. 2.  util包下, Date是sql包下三种时间类 ...

  3. mysql基础,索引

  4. python3.X中pickle类的用法(cPickle模块移除了)

    1.python3.x中移除了cPickle模块,可以使用pickle模块代替.最终我们将会有一个透明高效的模块. 2.因为存储的是对象,必须使用二进制形式写进文件 #!/usr/bin/python ...

  5. chroot: cannot run command `/bin/bash': No such file&nbs

    最近在使用chroot去重新的挂载一个根目录,总是出现上面的问题,很烦,好久了没有解决, 然后自己就写了一个复制依赖库的脚本,然后发现可以切换了,然后就重新试着去挂载根目录 终于发现了原因. ---- ...

  6. 修改 cmd 字体为 Consolas

    windows 下的 cmd 窗口默认的字体有点难看,长时间使用操作 node.js 有点小疲劳,可以修改注册表替换字体为 Consolas,并且可以全屏 cmd 窗口,代码如下: Windows R ...

  7. 15.5,centos下redis安全相关

      博文背景: 由于发现众多同学,在使用云服务器时,安装的redis3.0+版本都关闭了protected-mode,因而都遭遇了挖矿病毒的攻击,使得服务器99%的占用率!! 因此我们在使用redis ...

  8. Dapper.Extension的基本使用

    前言    上一篇随笔写了Dapper的简单的使用,这次写一下Dapper.Extension的使用,它是Dapper的简单的封装扩展,可以通过实例化的对象赋值后进行增删改的操作以及分页,但是却不能进 ...

  9. MySQL Group Replication数据安全性保障

    本文来自数据库内核专栏 在之前的文章中,介绍了MGR对数据可靠性.可用性和一致性的实现方案.简单来说,MGR通过基于paxos协议的多副本来实现数据的可靠性,通过多副本上的majority机制来实现可 ...

  10. javascript检测数组

    在ECMAScript5中的数组已经引入了isArray方法,该方法的目的就是检测变量是否为数组. 但是对于ie6.7等古老的浏览器是没有这样的方法的,在Zakas写的一本书上摘到一个函数,基本能优雅 ...