BZOJ2212【POI2011】ROT:Tree Rotation 线段树合并
题意:
给一棵n(1≤n≤200000个叶子的二叉树,可以交换每个点的左右子树,要求叶子遍历序的逆序对最少。
分析:
求逆序对我们可以想到权值线段树,所以我们对每个点建一颗线段树(为了避免空间爆炸,采取动态开点的科技)
两个子节点可以交换,于是我们可以递归,自底向上贪心解决问题,每次线段树合并,在合并时,统计交换左右子节点后,横跨当前位置的逆序对数量,以及不交换子节点的情况下的这个数量,将更优的计入答案。这道问题就圆满解决了。
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=;
struct node{ll v;int ls,rs;}t[N*];
int n,m,tot=,cnt=,nm[N],ls[N],rs[N],rt[N];
ll ans=,ans1,ans2;
void read(int x){
scanf("%d",&nm[x]);
if(!nm[x]) read(ls[x]=++cnt),
read(rs[x]=++cnt);return ;
} void update(int &x,int l,int r,int v){
if(!x) x=++tot;int mid=l+r>>;
if(l==r){t[x].v=;return ;}
if(v<=mid) update(t[x].ls,l,mid,v);
else update(t[x].rs,mid+,r,v);
t[x].v=t[t[x].rs].v+t[t[x].ls].v;
} int merge(int x,int y){
if(!x||!y) return x|y;
ans1+=(ll)t[t[x].rs].v*t[t[y].ls].v;
ans2+=(ll)t[t[x].ls].v*t[t[y].rs].v;
t[x].ls=merge(t[x].ls,t[y].ls);
t[x].rs=merge(t[x].rs,t[y].rs);
t[x].v=t[t[x].ls].v+t[t[x].rs].v;
return x;
} void dfs(int x){
if(!x) return ;
dfs(ls[x]);dfs(rs[x]);
if(!nm[x]){
ans1=ans2=;
rt[x]=merge(rt[ls[x]],rt[rs[x]]);
ans+=min(ans1,ans2);
} return ;
} int main(){
scanf("%d",&n);read();
for(int i=;i<=cnt;i++)
if(nm[i]) update(rt[i],,n,nm[i]);
dfs();printf("%lld\n",ans);
return ;
}
线段树合并
BZOJ2212【POI2011】ROT:Tree Rotation 线段树合并的更多相关文章
- BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对
原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...
- bzoj2212[Poi2011]Tree Rotations [线段树合并]
题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...
- 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]
题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...
- 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 ...
随机推荐
- Entity Framework Code First 迁移
Entity Framework CodeFirst数据迁移 http://www.cnblogs.com/aehyok/p/3325459.html Entity Framework Code Fi ...
- 立体渲染 Volumetric Rendering
基础概念 在3D游戏引擎中,球体.立方体以及所有其它复杂的集合体都是由三角面片组成的.引擎只会渲染物体的表面,比如球体,半透明物体等.整个世界由各种空壳构成. 立体渲染(Volumetric Rend ...
- bzoj 2111: [ZJOI2010]Perm 排列计数【树形dp+lucas】
是我想复杂了 首先发现大于关系构成了一棵二叉树的结构,于是树形dp 设f[i]为i点的方案数,si[i]为i点的子树大小,递推式是\( f[i]=f[i*2]*f[i*2+1]*C_{si[i]-1} ...
- [Swift通天遁地]一、超级工具-(5)使用UIWebView(网页视图)加载本地页面并调用JavaScript(脚本)代码
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [SDOI2013]spring
Description Input Output Sample Input 3 3 1 2 3 4 5 6 1 2 3 0 0 0 0 0 0 4 5 6 Sample Output 2 HINT 容 ...
- 51nod 1874 字符串排序
1874 字符串排序 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 定义一个字符串的无序度为所有位置后面的字母比该位置的字母小的总数之和.比如&q ...
- Android SQLite(1)简单示例-增,删,改,查
1.主要核心类,Sqlite编程要继承SQLiteOpenHelper import android.content.Context; import android.database.sqlite.S ...
- SpringCloud开发学习总结(七)—— 声明式服务调用Feign(一)
在实践的过程中,我们会发现在微服务架构中实现客户端负载均衡的服务调用技术Spring Cloud Ribbon<SpringCloud开发学习总结(四)—— 客户端负载均衡Ribbon> ...
- jsp的简介
https://www.w3cschool.cn/jsp/jsp-intro.html
- 228 Summary Ranges 汇总区间
给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7]输出: ["0->2","4->5",& ...