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 [题目大意] 给出一棵二叉树,每个叶节点上有一个权值,现在可以任意交换左右儿子, ...
随机推荐
- java代码输出质因数
package com.badu; import java.util.Scanner; //分解质因数问题: //从键盘输一个数, //首先最小质因数为2 //n不能被2整除时, //n能被2整除时, ...
- df 命令-显示目前磁盘剩余的磁盘空间
linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...
- postgresql 主从复制并切换
1 环境 192.168.19.145 Kylin 3.3 mysqlhq 9.5.2 psql_master192.168.19.227 Kylin 3.3 mysql3 9.5.2 p ...
- linux uid/euid/suid
Each UNIX process has 3 UIDs associated to it. Superuser/root is UID=0. UID Read UID. It is of the u ...
- Flash 零日漏洞复现(CVE-2018-4878)
项目地址:https://github.com/Sch01ar/CVE-2018-4878.git 影响版本为:Adobe Flash Player <= 28.0.0.137 攻击机器IP:1 ...
- 如何通过outline为SQL语句指定执行计划
创建测试表 以用户jyu连接,创建测试表 SQL> conn jyu/jyu; Connected. SQL> create table t (id number, name varcha ...
- DAY13-前端之jQuery
jQuery jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行 ...
- Javascript面向对象(二):构造函数的继承
这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个" ...
- Mac系统下MySql下载MySQL5.7及详细安装流程
一.在浏览器当中输入以下地址 https://dev.mysql.com/downloads/mysql/ 二.进入以下界面:直接点击下面位置 ,选择跳过登录 点过这后直接下载. 三.下载完成后 ...
- 使用Aspectj 的配置文件方式进行aop操作