[bzoj2212]Tree Rotations(线段树合并)
解题关键:线段树合并模板题。线段树合并的题目一般都是权值线段树,因为结构相同,求逆序对时,遍历权值线段树的过程就是遍历所有mid的过程,所有能求出所有逆序对。
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
int n,sz,seg;
ll ans,cnt1,cnt2;
int v[],l[],r[],root[];
int sum[],ls[],rs[];
//动态开点线段树
//int new_node(){ return ++sz; } void readtree(int x){
scanf("%d",&v[x]);
if(!v[x]){
l[x]=++sz;
readtree(l[x]);
r[x]=++sz;
readtree(r[x]);
}
} void pushup(int k){
sum[k]=sum[ls[k]]+sum[rs[k]];
} void build(int &k,int l,int r,int val){
if(!k)k=++seg;
if(l==r){sum[k]=;return;}
int mid=(l+r)>>;
if(val<=mid)build(ls[k],l,mid,val);
else build(rs[k],mid+,r,val);
pushup(k);
} int merge(int x,int y){
if(!x)return y;
if(!y)return x;
cnt1+=(ll)sum[rs[x]]*sum[ls[y]];
cnt2+=(ll)sum[ls[x]]*sum[rs[y]];
ls[x]=merge(ls[x],ls[y]);
rs[x]=merge(rs[x],rs[y]);
pushup(x);
return x;
}
void solve(int x){
if(!x)return;
solve(l[x]);
solve(r[x]);
if(!v[x]){
cnt1=cnt2=;
root[x]=merge(root[l[x]],root[r[x]]);
ans+=min(cnt1,cnt2);
}
}
int main(){
scanf("%d",&n);
++sz;
readtree();
for(int i=;i<=sz;i++)
if(v[i])build(root[i],,n,v[i]);
solve();
printf("%lld",ans);
return ;
}
[bzoj2212]Tree Rotations(线段树合并)的更多相关文章
- bzoj2212 Tree Rotations 线段树合并+动态开点
题目传送门 思路: 区间合并线段树的题,第一次写,对于一颗子树,无论这个子树怎么交换,都不会对其他子树的逆序对造成影响,所以就直接算逆序对就好. 注意叶子节点是1到n的全排列,所以每个权值都只会出现1 ...
- 【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> ...
- BZOJ.2212.[POI2011]Tree Rotations(线段树合并)
题目链接 \(Description\) 给定一棵n个叶子的二叉树,每个叶节点有权值(1<=ai<=n).可以任意的交换两棵子树.问最后顺序遍历树得到的叶子权值序列中,最少的逆序对数是多少 ...
- 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 ...
- BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...
- BZOJ2212【POI2011】ROT:Tree Rotation 线段树合并
题意: 给一棵n(1≤n≤200000个叶子的二叉树,可以交换每个点的左右子树,要求叶子遍历序的逆序对最少. 分析: 求逆序对我们可以想到权值线段树,所以我们对每个点建一颗线段树(为了避免空间爆炸,采 ...
随机推荐
- HTML 按钮换肤
.button2{ background-image: url(images/input-bg.jpg); width: 83px; height: 31px; border: none 0px; f ...
- yum安装postgresql
https://wiki.postgresql.org/wiki/YUM_Installation
- yum方式安装的Apache目录详解和配置说明
在对httpd.conf文件进行解读之前,首先了解一下Redhat9中Apache服务器默认配置的一些基本信息:配置文件:/etc/httpd/conf/http.conf1)"/etc/h ...
- Service的用法
基本用法: 1.创建一个类继承Service类,并重写onBind() 2.重写其他方法:onCreate().onStartCommand().onDestory() 3.在AndroidManif ...
- sklearn中的predict与predict_proba的区别(得到各条记录每个标签的概率(支持度))
假定在一个k分类问题中,测试集中共有n个样本.则: predict返回的是一个大小为n的一维数组,一维数组中的第i个值为模型预测第i个预测样本的标签: predict_proba返回的是一个n行k列的 ...
- 缺乏libaio包导致报The server quit without updating PID file
背景: 直接解压安装mysql5.7.18,解压mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz,直接拷贝另外一台数据库的数据目录,启动mysql过程无日志输出,报E ...
- python 绘图库 Matplotlib
matplotlib官方文档 使用Matplotlib,能够轻易生成各种图像,例如:直方图.波谱图.条形图.散点图等. 入门代码实例 import matplotlib.pyplot as plt i ...
- CentOS 7 创建桌面快捷方式
一,在桌面新建一个文件 文件名随意,但必须带有.desktop的后缀名, 以Eclipse为例 vi /home/lenmom/Desktop/eclipse.desktop 其中: lenmom 是 ...
- 请描述一下 cookies,sessionStorage和localStorage的区别?
cookie在浏览器和服务器间来回传递. sessionStorage和localStorage不会sessionStorage和localStorage的存储空间更大:sessionStorage和 ...
- Springmvc ajax请求400
转载做记录 传JSON对象 前端 function test () { var param = {username : "yitop"}; $.ajax({ timeout : 2 ...