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 ...
随机推荐
- hdu1848 sg打表
果然是神器. #include<stdio.h> #include<string.h> #define maxn 1002 ],sg[maxn],hash[maxn]; voi ...
- 2018-8-9-win10-uwp-装机必备应用-含源代码
title author date CreateTime categories win10 uwp 装机必备应用 含源代码 lindexi 2018-8-9 9:7:31 +0800 2018-8-9 ...
- 免费的容器架构可视化工具 | 阿里云应用高可用服务 AHAS 发布重大新特性
工具下载链接:点这里.活动发布链接:点这里. 采用容器服务后,了解容器之间的关系及依赖是一个比较有挑战的问题.容器化改造后的实际架构模型可能与预想的架构存在较大的差异,架构师或系统运维人员需要精确地了 ...
- mysql 连接慢的问题
现象: 今发现站点訪问数据库变慢,经查,查询数据库非常快,连接数据库比較耗时. 解决的方法: 在mysql的配置文件my.cnf中,在[mysqld]以下加上这个配置就能够了. 附录:[mysqld] ...
- Hbase API: 写入Bigtable.
- mysql数据库之存储引擎
mysql存储引擎概述 什么是存储引擎? MYSQL中的数据用各不相同的技术 ...
- AtCoder Beginner Contest 075 C Bridge(割边)
求割边个数.Tarjan的板子.. #include <bits/stdc++.h> using namespace std; const int MAXN = 55; const int ...
- oracle用>=替代>
如果DEPTNO上有一个索引, 高效: SELECT * FROM EMP WHERE DEPTNO >=4 低效: SELECT * FROM EMP WHERE DEPTNO >3 两 ...
- js写出你的名字的拼音,判断哪个字母出现的最多
function fn(str) { var obj = {}; for (var i = 0; i < str.length; i++) { if (!obj[str.charAt(i)]) ...
- Element-ui学习笔记2
1.Button按钮 <el-button> type属性可以修改按钮主题 type='text'时为文字按钮 plain,round,circle可以修改按钮样式,plain为朴素按钮, ...