【Luogu】P3521ROT-Tree Rotations(线段树合并)
神奇的线段树合并qwq 不过就思路而言很好想……
观察到一棵树无论怎么交换两棵左右子树,子树内部的最优逆序对并没影响……决策只影响左右子树之间的逆序对……
于是线段树合并直接乱搞就好啦
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#define mid ((l+r)>>1)
#define check(x) if(x==0) x=++tot;
#define maxn 400200
using namespace std;
inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} long long ls[maxn];
long long rs[maxn];
long long q[maxn];
long long root[maxn];
long long lc[maxn*];
long long rc[maxn*];
long long tree[maxn*];
long long f[maxn];
long long c,w;
long long ans;
long long n,tot,cnt; inline void pushup(long long rt){ tree[rt]=tree[lc[rt]]+tree[rc[rt]]; } void update(long long o,long long l,long long r,long long &rt){
check(rt);
if(l==r){
tree[rt]++; return;
}
if(o<=mid) update(o,l,mid,lc[rt]);
else update(o,mid+,r,rc[rt]);
pushup(rt);
} void gettree(long long &x){
x=++cnt;
q[x]=read();
if(q[x]) return;
gettree(ls[x]);
gettree(rs[x]);
return;
} long long merge(long long x,long long y){
if(x==) return y;
if(y==) return x;
c+=tree[rc[x]]*tree[lc[y]];
w+=tree[lc[x]]*tree[rc[y]];
lc[x]=merge(lc[x],lc[y]);
rc[x]=merge(rc[x],rc[y]);
pushup(x);
return x;
} void calc(long long x){
if(q[x]) return;
calc(ls[x]);
calc(rs[x]);
c=w=;
root[x]=merge(root[ls[x]],root[rs[x]]);
ans+=min(c,w);
return;
} void build(long long x){
if(q[x]){
update(q[x],,n,root[x]);
return;
}
build(ls[x]);
build(rs[x]);
return;
} long long Root; int main(){
n=read();
gettree(Root);
build();
calc();
printf("%lld",ans);
return ;
}
【Luogu】P3521ROT-Tree Rotations(线段树合并)的更多相关文章
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- bzoj2212[Poi2011]Tree Rotations [线段树合并]
题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...
- BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对
原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...
- BZOJ.2212.[POI2011]Tree Rotations(线段树合并)
题目链接 \(Description\) 给定一棵n个叶子的二叉树,每个叶节点有权值(1<=ai<=n).可以任意的交换两棵子树.问最后顺序遍历树得到的叶子权值序列中,最少的逆序对数是多少 ...
- [bzoj2212]Tree Rotations(线段树合并)
解题关键:线段树合并模板题.线段树合并的题目一般都是权值线段树,因为结构相同,求逆序对时,遍历权值线段树的过程就是遍历所有mid的过程,所有能求出所有逆序对. #include<iostream ...
- Bzoj P2212 [Poi2011]Tree Rotations | 线段树合并
题目链接 通过观察与思考,我们可以发现,交换一个结点的两棵子树,只对这两棵子树内的节点的逆序对个数有影响,对这两棵子树以外的节点是没有影响的.嗯,然后呢?(っ•̀ω•́)っ 然后,我们就可以对于每一个 ...
- bzoj2212/3702 [Poi2011]Tree Rotations 线段树合并
Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...
- bzoj2212 Tree Rotations 线段树合并+动态开点
题目传送门 思路: 区间合并线段树的题,第一次写,对于一颗子树,无论这个子树怎么交换,都不会对其他子树的逆序对造成影响,所以就直接算逆序对就好. 注意叶子节点是1到n的全排列,所以每个权值都只会出现1 ...
- BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...
- [POI2011]ROT-Tree Rotations 线段树合并|主席树 / 逆序对
题目[POI2011]ROT-Tree Rotations [Description] 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有\(n\)个叶子节点,满足这些权值为 ...
随机推荐
- 使用Java connector消费ABAP系统的函数
Java Connector(JCO)环境的搭建:Step by step to download and configure JCO in your laptop 我的ABAP系统有个函数名叫ZDI ...
- UVALive 3942 Remember The Word (Tire)
状态是DAG,因此方案用dp统计,dp[i] = sum(dp[i+len(x)]),x是以i开头的前缀且是单词,关键在于快速判断一个前缀是不是单词,可用Trie. 每一次转移的复杂度是O(maxle ...
- unbuntu&vim&Kali的各种小知识
1. vmware workstation 15.0.0 2.ubuntu-18.10-desktop 使用网络地址转换 VMware workstation 1.ctrl+alt 返回 unbu ...
- nginx之HTTP模块配置
listen 指令只能使用与server字段里 如果本地调用可以监听本地Unix套接字文件,性能更加,因为不用走内核网络协议栈 listen unix:/var/run/nginx.sock; ...
- linux程序安装及包管理
程序包的封装类型: RPM软件包:扩展名为“.rpm”,使用rpm命令安装. DEB软件包:扩展名为“.deb”,使用DPKG包管理器. 源代码软件安装:程序员开发完成的原始代码,一般制作成“.tar ...
- java abstraction and encapsulation
How is Abstraction different from Encapsulation? Abstraction happens at class level design. It resul ...
- Python学习记录4(语句)
赋值语句 序列解包 条件语句 语句块 布尔变量 条件执行和if语句 条件运算符 循环 while语句 for循环 迭代工具 跳出循环 break continue while truebreak语句 ...
- How to Install PhantomJS on Ubuntu 16.04
Introduction PhantomJS is a scripted, headless browser that can be used for automating web page inte ...
- python3.7 内置函数整理
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 内置函数整理 #abs(x) #返回数字的绝对值. 参数可以是整 ...
- LeetCode(152) Maximum Product Subarray
题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...