[BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
题目链接: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 【线段树合并】的更多相关文章
- BZOJ.2212.[POI2011]Tree Rotations(线段树合并)
题目链接 \(Description\) 给定一棵n个叶子的二叉树,每个叶节点有权值(1<=ai<=n).可以任意的交换两棵子树.问最后顺序遍历树得到的叶子权值序列中,最少的逆序对数是多少 ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- Bzoj P2212 [Poi2011]Tree Rotations | 线段树合并
题目链接 通过观察与思考,我们可以发现,交换一个结点的两棵子树,只对这两棵子树内的节点的逆序对个数有影响,对这两棵子树以外的节点是没有影响的.嗯,然后呢?(っ•̀ω•́)っ 然后,我们就可以对于每一个 ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- bzoj2212[Poi2011]Tree Rotations [线段树合并]
题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...
- BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对
原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...
- bzoj2212/3702 [Poi2011]Tree Rotations 线段树合并
Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...
- BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...
- bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...
随机推荐
- 给Chrome和Firefox添加js脚本作为插件的方法
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5973141. ...
- PullToRefresh的个性化扩展
一:实现区别下拉刷新和上拉加载 参考资料:http://blog.csdn.net/losetowin/article/details/18261389 在PullToRefresh的类库的com.h ...
- Magento中直接使用SQL语句
原理: magento是基于Zend Framework的,所以底层用的还是zend的zend db 在文件app/code/core/Mage/Catalog/model/Resource/Eav ...
- PHP之会话控制小结
会话控制是一种跟踪用户的通信方式,使用会话控制主要基于以下几点:由于http协议的无状态性,使得不能通过协议来建立两次请求之间的关联:对于通常的页面之间的数据传递方式get和post而言,主要处理参数 ...
- ASP判断文件地址是否有效
<% Response.Write("<head><style><!--span{ font-size: 9pt }--></style> ...
- springmvc错误 Spring3.X jdk8 java.lang.IllegalArgumentException
最近在学习springmvc--碰到一个特别蛋疼的错误 javax.servlet.ServletException: Servlet.init() for servlet springMVC thr ...
- hive metastore异常 org.apache.thrift.protocol.TProtocolException: Missing version in readMessageBegin, old client
hiveserver2的端口是10000hive.metastoe.uris 的端口9083改为10000之后 beelien 连接hiveserver2报错 Error: Could not ope ...
- Sqlserver 理解子查询
理解子查询: 理解子查询: 多表连接查询往往也可以用子查询进行替代 子查询本质是嵌套进其他 select update insert Delete 语句的一个被限制的select语句,在子查询中,只有 ...
- Swift轻松入门——基本语法介绍和详细地Demo讲解(利用WebView打开百度、新浪等网页)
转载请务必注明出处(all copyright reserved by iOSGeek) 本文主要分为两个部分,第一部分介绍Swift的基本语法,第二部分讲解一个利用WebView来打开百度.sina ...
- jquery个人笔记
一.链式操作 <!DOCTYPE html> <html> <head> <title></title> <script src = ...