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 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
随机推荐
- 【CC2530入门教程-01】CC2530微控制器开发入门基础
[引言] 本系列教程就有关CC2530单片机应用入门基础的实训案例进行分析,主要包括以下6部分的内容:[1]CC2530微控制器开发入门基础.[2]通用I/O端口的输入和输出.[3]外部中断初步应用. ...
- C# 获取一段日期内的工作日
/// <summary> /// 根据指定时间段计算工作日天数 /// </summary> /// <param name="firstDay"& ...
- poj 3104 Drying(二分查找)
题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- Perl6 Bailador框架(7):模版编写
先看一个例子: use v6; use Bailador; my $data = ' <form action="", method="get"> ...
- ThinkSnS v4后台任意文件下载漏洞
漏洞文件: /apps/admin/Lib/Action/UpgradeAction.class.php 主要问题还是出现在了180行直接将远程获取到的图片直接保存. 文中可见并没有做任何的对$dow ...
- nmap导出处理脚本
import sys log = open("result.gnmap","r") xls = open("output.csv",&quo ...
- ZOJ 3537 Cake 求凸包 区间DP
题意:给出一些点表示多边形顶点的位置(如果多边形是凹多边形就不能切),切多边形时每次只能在顶点和顶点间切,每切一次都有相应的代价.现在已经给出计算代价的公式,问把多边形切成最多个不相交三角形的最小代价 ...
- java===java基础学习(6)---流程控制,for,if,switch,continue,break
注意点: for循环的用法和python截然不同,注意格式 switch~,switch对应的case每当执行完毕都要break,由于基本不怎么用switch,所以作为了解. 中断流程控制语句,请考虑 ...
- HDU 6146 Pokémon GO DP,计数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6146 题意:~ 解法:原题..http://blog.csdn.net/y990041769/arti ...
- iOS APP程序启动原理
UIApplication 程序启动原理 一个应用程序运行就必须要有一个进程,一个进程至少要有一个线程,我们把这个线程叫做主线程,主线程开启之后会开启一个主运行循环,如果不开启一个运行循环,程序开启了 ...