LuoguP3521 [POI2011]ROT-Tree Rotations
P3521 [POI2011]ROT-Tree Rotations
题目大意:
给一棵\((1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少。
我们发现交换两个子树并不会影响某个子树内的逆序对个数,只会对两个子树之间的逆序对产生影响.
所以我们将换与不换的逆序对求出来,取个最小值
将两颗线段树合并就好了
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cctype>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 4e5 + 3;
struct node{
int sum;
int lc,rc;
}a[N * 30];
int root;
int n,t,cnt = 1;
int rt[N];
LL ans1,ans2,ans;
inline int read(){
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
inline void pushup(int u){
a[u].sum = a[a[u].lc].sum + a[a[u].rc].sum;
}
inline void ins(int &u,int l,int r,int x){
// printf("%d %d %d\n",l,r,x);
if(!u) u = ++t;
if(l == r){
a[u].sum++;
return ;
}
int mid = (l + r) >> 1;
if(x <= mid) ins(a[u].lc,l,mid,x);
else ins(a[u].rc,mid + 1,r,x);
pushup(u);
}
inline void merge(int &u1,int u2,int l,int r){
if(!u1){u1 = u2;return ;}
if(!u2) return ;
if(l == r){
a[u1].sum += a[u2].sum;
return ;
}
int mid = (l + r) >> 1;
ans1 += 1ll * a[a[u1].lc].sum * a[a[u2].rc].sum;
ans2 += 1ll * a[a[u2].lc].sum * a[a[u1].rc].sum;
merge(a[u1].lc,a[u2].lc,l,mid);
merge(a[u1].rc,a[u2].rc,mid + 1,r);
pushup(u1);
}
inline void solve(int pos){
int x = read();
if(!x){
int lc = ++cnt;solve(lc);
int rc = ++cnt;solve(rc);
merge(rt[pos],rt[lc],1,n);
ans1 = ans2 = 0;
merge(rt[pos],rt[rc],1,n);
ans += min(ans1,ans2);
}
else{
ins(rt[pos],1,n,x);
return ;
}
}
int main(){
n = read();
solve(1);
printf("%lld\n",ans);
return 0;
}
LuoguP3521 [POI2011]ROT-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 ...
- POI2011 Tree Rotations
POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...
- [bzoj3702/2212][Poi2011]二叉树/Tree Rotations_线段树
二叉树 Tree Rotations bzoj-3702 bzoj-2212 Poi-2011 题目大意:现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有n个叶子节点,满足 ...
- bzoj 2212 Tree Rotations
bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...
- 线段树合并(【POI2011】ROT-Tree Rotations)
线段树合并([POI2011]ROT-Tree Rotations) 题意 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有nn个叶子节点,满足这些权值为1-n1-n的一个 ...
- 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 ...
随机推荐
- pl/sql基础知识—定义并使用变量
n 介绍 在编写pl/sql程序是,可以定义变量和常量:在pl/sql程序中包括有: ①标量类型(scalar) ②复合类型(composite) ③参照类型(reference) ④lob(lar ...
- ZOJ 3956 Course Selection System [01背包]
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 题意:就是给你Hi,Ci的值,问怎么取使得下面那个式子的值最大: 理 ...
- Python3 写的远程批量修改文件内容的脚本
一.说明: 1.利用Python的paramiko模块,调用远程的shell命令去修改相应文件. 2.有一个专用配置文件,列出服务器清单. 3.Python循环读取配置文件的服务器IP去连接它,并执行 ...
- MySQL中使用LIMIT进行分页的方法
一.分页需求: 客户端通过传递start(页码),pageSize(每页显示的条数)两个参数去分页查询数据库表中的数据,那我们知道MySql数据库提供了分页的函数limit m,n,但是该函数的用法和 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十二章:几何着色器(The Geometry Shader)
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十二章:几何着色器(The Geometry Shader) 代码工 ...
- MySQL的安装问题总结--终极解决方案
MySQL安装 选择:custom 自定义 更改路径 安装到其他盘 选择:launch configuration finish 进行配置 如果忘记选择 找 "E:\Program Fil ...
- sql —— like
用于在 WHERE 子句中搜索列中的指定模式. 原表: 一.% %表示任何字符出现任意次数. 1.以某个字符串开头的数据 2.包含某个字符串的数据 3.以某个字符串结尾的数据 二._ 只适用于匹配单个 ...
- myeclipse 如何更改java jsp 等文件的编码方式
java的编码方式: 1.window——>preference 2.General——>Workspace,右边[Text file encoding]选择编码后,点击[OK]. jsp ...
- redux之createStore方法底层封装模拟
首先在看代码之前让我们一起回顾下redux的思想吧 首先redux就是一个MVC思想的框架,他总体是遵循数据的单向流动自顶向下流动 在我们仓库中有一个initState用来存储着我们的初始数据 另 ...
- 使用DECLARE定义条件和处理程序
定义条件和处理程序是事先定义程序执行过程中可能遇到的问题,并且可以在处理程序中定义解决这些问题的办法,可以简单理解 为异常处理,这种方式可以提前预测可能出现的问题,并提出解决办法,从而增强程序健壮性. ...