【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\)个叶子节点,满足这些权值为 ...
随机推荐
- 2017-3-7-lint82single-number
2017-3-7-lint82single-number 在河之洲 算法 小书匠 problem 82single-number/ solution int singleNumber(vector&l ...
- freespace
这是一个总结freespace的博客,还不错,但算法目前看来比较老:https://blog.csdn.net/u010213183/article/details/70786516
- VIM+ctags+cscope用法
使用vim + cscope/ctags,就能够实现Source Insight的功能,可以很方便地查看分析源代码. 关键词: vim, cscope, ctags, tags 1. 查看vi ...
- Python——数据类型
如果是C语言,Java使用一个变量之前需要声明,数字,字符,布尔等都有特定的声明方式,前端中常用的js中都要使用var,而python中直接用就行了 比如: 虽然是这样使用,但其实是当你给一个变量赋值 ...
- Linux curl命令中,HTTP 302处理
在Linux中使用curl命令时,偶尔会遇到一些URL跳转到新的URL,即HTTP中的3XX(redirection,重定向 ). $curl -s -I $URL > log 这时在返回的报文 ...
- UIKeyboardType
typedef NS_ENUM(NSInteger, UIKeyboardType) { UIKeyboardTypeDefault, // Default type for the current ...
- 重装vs2008遇到的问题
由于前几天办公室电脑dhcp服务挂了,wifi网线都上不了网,很无奈只能重装了系统.于是VS2008也要重装,之前一直用的都是前一个同事留下来的软件,没自己装过,自己装的时候踩了坑,记录一下. 重装了 ...
- [].indexOf.call()学习
今天看到闭包一道题,就是一个li列表,点击列表控制台输出对应的索引.这里考察了var的作用域问题和闭包对外部变量的引用问题,有几种解决方法. html: <ul> <li>te ...
- Linux-Java安装
安装 yum -y install java-1.8.0-openjdk* Tomcat安装:到官网http://tomcat.apache.org/,然后找到Tomcat9下载地址 http://m ...
- salt 模板
http://www.mamicode.com/info-detail-2297406.html