POI2011 Tree Rotations
POI2011 Tree Rotations
给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树。要求前序遍历叶子的逆序对最少。
由于对于当前结点x,交换左右子树,对于范围之外的逆序对个数并没有影响,所以可以进行线段树合并,合并时统计l在左边还是在右边更优。
#include <cstdio>
#include <cctype>
using namespace std;
typedef long long LL;
inline void read(int &x){
char ch; x=0;
for (; ch=getchar(), !isdigit(ch););
for (x=ch-48; ch=getchar(), isdigit(ch);)
x=(x<<3)+(x<<1)+ch-48;
}
inline void read(LL &x){
char ch; x=0;
for (; ch=getchar(), !isdigit(ch););
for (x=ch-48; ch=getchar(), isdigit(ch);)
x=(x<<3)+(x<<1)+ch-48;
}
inline LL min(LL a,LL b) { return a > b?b : a; }
const int maxn = 2e5+5;
struct node {
int seg,ls,rs;
} a[maxn*30];
LL ANS = 0,ans1 = 0,ans2 = 0;
int n;
int cnt = 0;
void modify(int &x,int l,int r, int pos) {
if(!x) x = ++cnt;
a[x].seg++;
int mid=l+r>>1;
if(l == r) return;
if(pos <= mid) modify(a[x].ls,l,mid,pos);
else modify(a[x].rs,mid+1,r,pos);
}
void merge(int &l,int r) {
if(!l || !r) { l+=r; return; }
a[l].seg += a[r].seg;
ans1 += (LL)a[a[l].rs].seg*a[a[r].ls].seg;
ans2 += (LL)a[a[l].ls].seg*a[a[r].rs].seg;
merge(a[l].ls,a[r].ls);
merge(a[l].rs,a[r].rs);
}
void solve(int &x) {
int t,ls,rs;
x = 0; read(t);
if(!t) {
solve(ls),solve(rs);
ans1 = ans2 = 0;
x = ls;
merge(x,rs);
ANS += min(ans1,ans2);
} else modify(x,1,n,t);
}
int main() {
read(n);
int t = 0;
solve(t);
printf("%lld\n",ANS);
return 0;
}
POI2011 Tree Rotations的更多相关文章
- BZOJ2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 391 Solved: 127[Submi ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- 2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- bzoj 2212: [Poi2011]Tree Rotations
Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...
- 【bzoj2212】[Poi2011]Tree Rotations 权值线段树合并
原文地址:http://www.cnblogs.com/GXZlegend/p/6826614.html 题目描述 Byteasar the gardener is growing a rare tr ...
- 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 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
- BZOJ 2212 [Poi2011]Tree Rotations(线段树合并)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2212 [题目大意] 给出一棵二叉树,每个叶节点上有一个权值,现在可以任意交换左右儿子, ...
随机推荐
- AngularJS:动画
ylbtech-AngularJS:动画 1.返回顶部 1. AngularJS 动画 AngularJS 提供了动画效果,可以配合 CSS 使用. AngularJS 使用动画需要引入 angula ...
- 11-09SQLserver 基础-数据库之汇总练习45题
设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表 ...
- UML在软件开发中各个阶段的作用和意义
经典的软件工程思想将软件开发分成5个阶段:需求分析,系统分析与设计,系统实现,测试及维护五个阶段. 之所以如此,是因为软件开发中饣含了物和人的因素,存在着很大的不确定性,这使得软件工程不可能像理想的, ...
- 基于IFC的施工过程模拟程序(4D BIM)
- Halcon将裁剪后的图像还原为原始大小
************************************************************* * Halcon将裁剪后的图像还原为原始大小 * Author: LiGua ...
- 关于PDF的一些书籍
PDF Explained: The ISO Standard for Document Exchange https://ssearch.oreilly.com/?q=PDF+
- hBase-thrift 实践(java)
参考官网: http://wiki.apache.org/hadoop/Hbase/ThriftApi 环境:hbase-0.98.1-cdh5.1.0,hadoop-2.3.0-cdh5.1.0,c ...
- Android 中的菜单 OptionsMenu的简单应用
OptionsMenu就是安卓手机中的菜单选项 首先 要实现对菜单的操作就要先重写OnCreateOptionsMenu(Menu menu)方法 通常有两种方法来实现对菜单中选项的添加 第一种是动态 ...
- Fiddler的Request Builder(Composer)模拟发送POST请求
传json格式: Parsed写: User-Agent: Fiddler Host: localhost:1455 Content-Type: application/json; charset=u ...
- SDUT 3343 数据结构实验之二叉树四:还原二叉树
数据结构实验之二叉树四:还原二叉树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定一棵 ...