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 [题目大意] 给出一棵二叉树,每个叶节点上有一个权值,现在可以任意交换左右儿子, ...
随机推荐
- 一些js和JQuery的方法
取值填值: js: get-- innerText, innerHTML, value; set-- innerText=?, innerHTML=?, value=?; JQuery: get-- ...
- wap手机端按下 松开 滑动事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- maven如何过滤占位符
今天遇到一个问题,就是properties文件中赋值用的这种形式${xxx},真正的值是配置在pom的profile中,但是未生效. 后来找到原因,原来是pom中少了一段代码: <build&g ...
- 侯捷STL学习(八)-- 深度探索deque
layout: post title: 侯捷STL学习(八) date: 2017-07-19 tag: 侯捷STL --- 第十八节 深度探索deque上 duque内存结构 分段连续,用户看起来是 ...
- mysql使用存储过程插入数据后,参数为中文的为?或乱码
最近了解了一下mysql存储过程,之前版本的mysql不支持存储过程,5.0版本后就可以支持存储过程的使用:恰好笔者下载使用版本为5.6.20: 做了一个给表插入数据的简单存储过程,发现打开表后汉字全 ...
- matlab学习笔记(3)
数据分析: 多项式: 多项式表示:p = [1 2 3 0]; //表示 1*x^3+2*x^2+3*x^1+0 ,系数从高次向低次项,0系数不能省略. roots函数:求解多项式的根.roots(p ...
- Class类动态加载类的用法
编译时刻加载类出现的问题:一个功能有错,所有功能都用不了 动态加载类:
- 无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_BIN" 之间的排序规则冲
在两个数据库之间进行复合查询时有时会出现如下错误: 无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_BIN&qu ...
- js 中的apply
之一------(函数的劫持与对象的复制)关于对象的继承,一般的做法是用复制法: Object.extend 见protpotype.js 的实现方法: Object.extend = functio ...
- 【总结整理】WebGIS学习-thinkGIS(二):关于level,比例尺scale,分辨率resolution
1.Level包含了一个resolution参数和一个scale参数 瓦片本身: 我们用arcgis切完图后,打开发布的服务或者打开config.xml配置文件,可以看到所切之图的相关配置.如图所示: ...