题目链接:BZOJ - 2212

题目分析

子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对。

左右子树内部的逆序对与是否交换左右子树无关,是否交换左右子树取决于交换后 “跨越 x 左子树与右子树的逆序对” 是否会减小。

因此我们要求出两种情况下的逆序对数,使用线段树合并,对每个节点建一棵线段树,然后合并的同时就求出两种情况下的逆序对。

代码

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm> using namespace std; inline void Read(int &Num)
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
Num = c - '0'; c = getchar();
while (c >= '0' && c <= '9')
{
Num = Num * 10 + c - '0';
c = getchar();
}
} typedef long long LL; inline LL gmin(LL a, LL b) {return a < b ? a : b;} const int MaxN = 400000 + 5, MaxNode = 4000000 + 5; int n, IndexT, Index, RT;
int A[MaxN], Tree[MaxN][2], Root[MaxN], T[MaxNode], Son[MaxNode][2]; LL Ans0, Ans1, Ans; void Read_Tree(int &x)
{
x = ++IndexT;
Read(A[x]);
if (A[x] != 0) return;
Read_Tree(Tree[x][0]);
Read_Tree(Tree[x][1]);
} inline void Update(int x)
{
T[x] = T[Son[x][0]] + T[Son[x][1]];
} void Insert(int &x, int s, int t, int Pos)
{
if (x == 0) x = ++Index;
if (s == t)
{
T[x] = 1;
return;
}
int m = (s + t) >> 1;
if (Pos <= m) Insert(Son[x][0], s, m, Pos);
else Insert(Son[x][1], m + 1, t, Pos);
Update(x);
} int Merge(int x, int y)
{
if (!x) return y;
if (!y) return x;
Ans0 += (LL)T[Son[x][1]] * (LL)T[Son[y][0]];
Ans1 += (LL)T[Son[x][0]] * (LL)T[Son[y][1]];
Son[x][0] = Merge(Son[x][0], Son[y][0]);
Son[x][1] = Merge(Son[x][1], Son[y][1]);
Update(x);
return x;
} void Solve(int x)
{
if (A[x]) return;
Solve(Tree[x][0]); Solve(Tree[x][1]);
Ans0 = Ans1 = 0;
Root[x] = Merge(Root[Tree[x][0]], Root[Tree[x][1]]);
Ans += gmin(Ans0, Ans1);
} int main()
{
scanf("%d", &n);
Read_Tree(RT);
for (int i = 1; i <= IndexT; ++i)
if (A[i] != 0) Insert(Root[i], 1, n, A[i]);
Solve(RT);
cout << Ans << endl;
return 0;
}

  

[BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】的更多相关文章

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

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

  2. BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )

    线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...

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

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

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

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

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

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

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

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

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

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

  8. BZOJ_2212_[Poi2011]Tree Rotations_线段树合并

    BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...

  9. bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...

随机推荐

  1. percona-toolkit工具检查MySQL复制一致性及修复

    利用percona-toolkit工具检查MySQL数据库主从复制数据的一致性,以及修复. 一.             pt-table-checksum检查主从库数据的一致性 pt-table-c ...

  2. 关于Eclipse插件开发-----加入首选项(preferencePages)

    选择主菜单"窗口---->首选项"命令打开"首选项"窗口.此窗口是Eclipse设置项的集中营, 修改plugin.xml文件,设置首选项的扩展点: pl ...

  3. Makefile的规则

    在讲述这个Makefile之前,还是让我们先来粗略地看一看Makefile的规则:最基本的编写规则的方法是从最终的源程序文件一个一个的查看源码文件.把它们要生成的目标文件作为目标,而C语言源码文件和源 ...

  4. PHP生成HTML页面顶部出现空白部分(&#65279字符?)

    参考了:PHP生成HTML页面顶部出现空白部分(#65279字符?)的解决办法 查看拼接两个Html,查看文件格式是否是UTF-8 无Bom,我的内容Html是UTF-8 + Bom.

  5. 搭建maven开发环境测试Hadoop组件HDFS文件系统的一些命令

    1.PC已经安装Eclipse Software,测试平台windows10及Centos6.8虚拟机 2.新建maven project 3.打开pom.xml,maven工程项目的pom文件加载以 ...

  6. JavaScript高级程序设计(第三版)学习,第一次总结

    Array类型 var arr = []; arr.length; //返回数组元素个数 改变length可以动态改变数组大小 检测数组 instanceof可以检测某个对象是否是数组,限制:只能是一 ...

  7. 【转】MyBatis学习总结(一)——MyBatis快速入门

    [转]MyBatis学习总结(一)——MyBatis快速入门 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC ...

  8. openvswitch安装和使用 --修订通用教程的一些错误

    1.下载openvswitch源文件,注意版本要适合操作系统内核. 推荐openvswitch2.0及其以上版本. 2.开始安装openvswitch cd openvswitch sudo ./bo ...

  9. iOS开发——百度云推送

    由于公司项目是集成的极光推送,详见下一篇博客. 集成百度推送大体相当,最好都参考官方文档集成,官方文档或官方网站教程是最好的博客. 百度Push服务SDK用户手册(iOS版) http://push. ...

  10. HDOJ 1422 重温世界杯 -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1422 Problem Description 世界杯结束了,意大利人连本带利的收回了法国人6年前欠他们 ...