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 ...
随机推荐
- bzoj1231 混乱的奶牛
Description 混乱的奶牛 [Don Piele, 2007] Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S ...
- sql —— check
CHECK 约束用于限制列中的值的范围. 原表: 现在要对性别这一列进行约束,只准值为男或者女. 步骤: 1. 2. 3. 4. 5. ctrl + s 保存一下, 现在不可以随意的修改性别了. 完成 ...
- nodeJs学习-15 mysql中间件下载与使用、基本用法
下载mysql中间件(客户端):cnpm install mysql 链接数据库.查询示例: const mysql=require('mysql'); //1.连接 //createConnecti ...
- iOS 通知观察者的被调函数不一定运行在主线程
Tony in iOS | 08/08/2013 iOS 通知观察者的被调函数不一定运行在主线程 今天修复Bug时候发现的一个小细节,记录下. 问题描述 事情是这样的:我在A视图(UITableVie ...
- 2019-6-23-开源项目使用-appveyor-自动构建
title author date CreateTime categories 开源项目使用 appveyor 自动构建 lindexi 2019-06-23 11:47:40 +0800 2019- ...
- Java练习 SDUT-3339_计算长方形的周长和面积(类和对象)
计算长方形的周长和面积(类和对象) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 设计一个长方形类Rect,计算长方形 ...
- HDU 5672 String【尺取法】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5672 题意: 有一个10≤长度≤1,000,000的字符串,仅由小写字母构成.求有多少个子串,包含有 ...
- cume_dist(),允许并列名次、复制名次自动空缺,取并列后较大名次,结果如22355778……
将score按ID分组排名:cume_dist() over(partition by id order by score desc)*sum(1) over(partition by id) 将sc ...
- 2018-2-13-WPF-只允许打开一个实例
title author date CreateTime categories WPF 只允许打开一个实例 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:2 ...
- HTML打印print
上代码: //打印 function printme() { global_Html = document.body.innerHTML; //document.body.innerHTML = do ...