BZOJ 2212 [Poi2011]Tree Rotations(线段树合并)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=2212
【题目大意】
给出一棵二叉树,每个叶节点上有一个权值,现在可以任意交换左右儿子,
使得逆序对最少,求最少的逆序对数量
【题解】
我们发现对于每个非叶节点来说,其贡献值为左右两个儿子的权值树上,
每个节点想反位置的数量和乘积,比如左儿子的权值树左节点和右儿子权值树的右节点相乘,
那么我们对于每个节点建立一颗权值线段树,仅保留非0链,
递归合并这些权值线段树,同时每次将相反位置数量乘积的最小值累加到答案即可
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N=400010,M=N*20;
typedef long long LL;
int n,a[N],cnt,Root,root[N];
int Tree[N][2];
LL Ans,Ans0,Ans1;
void Read_Tree(int &x){
x=++cnt;
scanf("%d",&a[x]);
if(a[x])return;
Read_Tree(Tree[x][0]);
Read_Tree(Tree[x][1]);
}
namespace Segment_Tree{
int tot;
struct node{int l,r,a,b,sum;}T[M];
void up(int x){T[x].sum=T[T[x].l].sum+T[T[x].r].sum;}
int build(int l,int r,int p){
int x=++tot;
T[x].a=l; T[x].b=r; T[x].sum=0;
if(l==r){T[x].sum=1;return x;}
int mid=(l+r)>>1;
if(p<=mid){T[x].l=build(l,mid,p);}
else{T[x].r=build(mid+1,r,p);}
return up(x),x;
}
int merge(int x,int y){
if(!x||!y)return x^y;
Ans0+=(LL)T[T[x].r].sum*(LL)T[T[y].l].sum;
Ans1+=(LL)T[T[x].l].sum*(LL)T[T[y].r].sum;
T[x].l=merge(T[x].l,T[y].l);
T[x].r=merge(T[x].r,T[y].r);
return up(x),x;
}
void dfs(int x){
if(a[x])return;
dfs(Tree[x][0]); dfs(Tree[x][1]);
Ans0=Ans1=0;
root[x]=merge(root[Tree[x][0]],root[Tree[x][1]]);
Ans+=min(Ans0,Ans1);
}
}
int main(){
scanf("%d",&n);
Read_Tree(Root);
for(int i=1;i<=cnt;i++)if(a[i]!=0)root[i]=Segment_Tree::build(1,n,a[i]);
Segment_Tree::dfs(Root);
printf("%lld\n",Ans);
return 0;
}
BZOJ 2212 [Poi2011]Tree Rotations(线段树合并)的更多相关文章
- BZOJ.2212.[POI2011]Tree Rotations(线段树合并)
题目链接 \(Description\) 给定一棵n个叶子的二叉树,每个叶节点有权值(1<=ai<=n).可以任意的交换两棵子树.问最后顺序遍历树得到的叶子权值序列中,最少的逆序对数是多少 ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- Bzoj P2212 [Poi2011]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个叶子的二叉树, ...
- bzoj2212/3702 [Poi2011]Tree Rotations 线段树合并
Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...
- BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...
- bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...
- [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
随机推荐
- YII 框架查询
基础查询 Customer::find()->one(); 此方法返回一条数据: Customer::find()->all(); 此方法返回所有数据: Customer::f ...
- CSS3禁止网页中文本被选中代码
通常大家会有js来实现,另一个方案就是,将-webkit-user-select 和-moz-user-select 的值设为none,这针对于移动用户,可能会很有用.请谨慎使用这个属性:因为大部分用 ...
- 4.0docker部署
设置容器的端口映射 -P :容器暴露的所有端口映射 -p :指定映射容器暴露的端口 Nginx部暑流程 docker run -p 80 --name web -t -i ubuntu /bin/b ...
- 随机森林(Random Forest)详解(转)
来源: Poll的笔记 cnblogs.com/maybe2030/p/4585705.html 1 什么是随机森林? 作为新兴起的.高度灵活的一种机器学习算法,随机森林(Random Fores ...
- [Linux]Linux printf 输出重定向【转】
转自:http://www.cnblogs.com/aaronLinux/p/6765145.html?utm_source=itdadao&utm_medium=referral 方法一 # ...
- 算法题之找出数组里第K大的数
问题:找出一个数组里面前K个最大数. 解法一(直接解法): 对数组用快速排序,然后直接挑出第k大的数.这种方法的时间复杂度是O(Nlog(N)).N为原数组长度. 这个解法含有很多冗余,因为把整个数组 ...
- Unsupported gpu architecture 'compute_20'
NVCC src/caffe/layers/reduction_layer.cunvcc fatal : Unsupported gpu architecture 'compute_20'Make ...
- LVS负载均衡DR模式
什么是集群? 一组相互独立的计算机,利用高速通信网络组成的一个计算机系统,对于客户机来说像是一个单一服务器,实际上是一组服务器.简而言之,一堆机器协同工作就是集群.集群的基本特点:高性能.高并发.高吞 ...
- ASP.NET Core学习链接
https://www.cnblogs.com/artech/p/dependency-injection-in-asp-net-core.html http://www.cnblogs.com/ar ...
- java字符串转换数值类型出现异常赋予默认值
http://blog.csdn.net/w47_csdn/article/details/77855126 可以自定义工具方法,例如: public static int parseInt(Stri ...