【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\)个叶子节点,满足这些权值为 ...
随机推荐
- 人脸识别 python调用face++ 功能测试
使用python调用face++的API,调用detect功能,识别人脸 首先进入face++官网注册,获得API Key和API Secret.使用官网提供的免费python api调用功能,提供了 ...
- 融云红包全新升级,让App用户更便捷地用“钱”交流感情!
随着移动互联网的飞速发展,如何增强社交关系.留住用户的心已成为移动社交化时代各类App持续探索的问题,除了接入即时通讯的能力,众多社交平台开始通过趣味性十足的红包功能为App中的社交场景赋能.当即时通 ...
- CF Gym 100187D Holidays (数学,递推)
题意:给n个元素,从n中选两个非空集合A和B.问有多少中选法? 递推: dp[n]表示元素个数为n的方案数,对于新来的一个元素,要么加入集合,要么不加入集合自成一个集合.加入集合有三种选择,A,B,E ...
- python解析xml实例
如下,一个银行卡打标签后导出的数据 <?xml version="1.0" encoding="ISO-8859-1"?> <annotati ...
- 在DataGridView控件中显示下拉列表
实现效果: 知识运用: DataGridViewComboBoxColumn类 //通过该类可以创建下拉列表样式的列 实现代码: private void Form1_Load(object send ...
- 标签input的value属性和placeholderde 区别
placeholder 顾名思义是一个占位符 在你的value为空的时候他才会显示出来,但是他本身并不是value,也不会被表单提交.
- java,编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz.
需求:编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz. package study01; public cla ...
- ssh整合思想 Spring分模块开发 crud参数传递 解决HTTP Status 500 - Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or(增加事务)
在Spring核心配置文件中没有增加事务方法,导致以上问题 Action类UserAction package com.swift.action; import com.opensymphony.xw ...
- graph-Dijkstra's shortest-path alogorithm
直接贴代码吧,简明易懂. 后面自己写了测试,输入数据为: a b c d e 0 1 4 0 2 2 1 2 3 1 3 2 1 4 3 2 1 1 2 3 4 2 4 5 4 3 1 也就是课本上1 ...
- 水题:UVa489-Hangman Judge
Hangman Judge Time limit 3000 ms Description In Hangman Judge, you are to write a program that judge ...