2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations
https://www.lydsy.com/JudgeOnline/problem.php?id=2212
分析:
线段树合并。
首先对每个节点建立一棵权值线段树,然后遍历整棵树,从叶子节点开始合并,每次合并两个节点的主席树,判断是否交换这两个节点,求出这两个节点分在前面所形成的逆序对。
求逆序对:对于主席树中的右子树一定比左子树大,所以每次合并一个节点时,直接用右子树的个数乘以左子树的个数,不断递归。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define fi(s) freopen(s,"r",stdin);
#define fo(s) freopen(s,"w",stdout);
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int A[N << ], T[N << ][], Root[N << ], sum[N * ], ls[N * ], rs[N * ];
int Index, tot, Rt;
LL Ans, Ans0, Ans1; void ReadTree(int &x) {
x = ++Index;
A[x] = read();
if (A[x]) return ;
ReadTree(T[x][]);
ReadTree(T[x][]);
} void Insert(int l,int r,int &rt,int p) {
rt = ++tot;
sum[rt] = ;
if (l == r) return ;
int mid = (l + r) >> ;
if (p <= mid) Insert(l, mid, ls[rt], p);
else Insert(mid + , r, rs[rt], p);
} int Merge(int x,int y) { // 合并x y
if (!x || !y) return x + y;
Ans0 += 1ll * sum[rs[x]] * sum[ls[y]]; // x在前
Ans1 += 1ll * sum[rs[y]] * sum[ls[x]]; // y在前
ls[x] = Merge(ls[x], ls[y]);
rs[x] = Merge(rs[x], rs[y]);
sum[x] += sum[y];
return x;
} void solve(int x) {
if (A[x]) return ;
solve(T[x][]); solve(T[x][]);
Ans0 = , Ans1 = ;
Root[x] = Merge(Root[T[x][]], Root[T[x][]]);
Ans += min(Ans0, Ans1);
} int main() { int n = read();
ReadTree(Rt);
for (int i=; i<=Index; ++i)
if (A[i]) Insert(, n, Root[i], A[i]);
solve(Rt);
cout << Ans << endl;
return ;
}
2212: [Poi2011]Tree Rotations的更多相关文章
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
		
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
 - [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
		
题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
 - bzoj 2212: [Poi2011]Tree Rotations
		
Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...
 - BZOJ 2212 [Poi2011]Tree Rotations(线段树合并)
		
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2212 [题目大意] 给出一棵二叉树,每个叶节点上有一个权值,现在可以任意交换左右儿子, ...
 - bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)
		
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...
 - 【BZOJ】2212: [Poi2011]Tree Rotations
		
题意 给一棵\(n(1 \le n \le 200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. 分析 可以发现如果交换非叶结点的左右子树,对子树内的交换无影响, ...
 - BZOJ.2212.[POI2011]Tree Rotations(线段树合并)
		
题目链接 \(Description\) 给定一棵n个叶子的二叉树,每个叶节点有权值(1<=ai<=n).可以任意的交换两棵子树.问最后顺序遍历树得到的叶子权值序列中,最少的逆序对数是多少 ...
 - BZOJ2212: [Poi2011]Tree Rotations
		
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 391 Solved: 127[Submi ...
 - 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
		
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
 
随机推荐
- UEditor 中配置可以跨域访问的图片路径
			
文档里很清楚:http://fex.baidu.com/ueditor/#server-path 进入配置文件 当域名不是直接配置到项目根目录时,例:http://a.com/b/c 域名下有两文件 ...
 - ezmorph-1.0.6.jar的作用
			
使用json时要依赖的jar文件,在使用jquery的json时,没有引用该文件的时候容易报错.
 - Android进阶笔记15:选用合适的IPC方式
			
1. 相信大家都知道Android进程间通信方式很多,比如AIDL.Messenger等等,接下来我就总结一下这些IPC方式优缺点. 2. IPC方式的优缺点和适用场景 3. 附加:使用Intent实 ...
 - 【[USACO15JAN]草鉴定Grass Cownoisseur】
			
这大概是我写过的除了树剖以外最长的代码了吧 首先看到有向图和重复经过等敏感词应该能想到先tarjan后缩点了吧 首先有一个naive的想法,既然我们要求只能走一次返回原点,那我们就正着反着建两遍图,分 ...
 - 第五章.MyBatis高级映射
			
5.1 新建数据库准备 CREATE TABLE `finacial_products` ( `product_id` ) NOT NULL AUTO_INCREMENT, `name` ) NO ...
 - alibaba--java规范
			
18. [推荐]final 可以声明类.成员变量.方法.以及本地变量,下列情况使用 final 关键字: 1) 不允许被继承的类,如:String 类. 2) 不允许修改引用的域对象,如:POJO 类 ...
 - 【题解】洛谷P1074 [NOIP2009TG] 靶形数独(DFS+剪枝)
			
洛谷P1074:https://www.luogu.org/problemnew/show/P1074 思路 这道题一看就是DFS 打一个分数表方便后面算分 我用x y z数组分别表示行 列 宫 是否 ...
 - jquery实现页面图片轮播
			
1.创建一个html页面 <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...
 - 微信小程序,搜索结果关键词高亮 wxml不能动态识别html标签
			
wxml中使用rich-text标签放置动态html标签 js:
 - Haroopad 中文不显示
			
https://blog.csdn.net/zgf19930504/article/details/51508111 1. 选择文件--> 偏好设置 2. 选择 编辑器--> 编辑--&g ...