原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html


题目传送门 - BZOJ2212


题意概括

  给一棵n(1≤n≤200000个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少。


题解

  线段树合并。

  博主很懒,题解不写了。

  这份代码是仿照别人的写的。


代码

#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std;
typedef long long LL;
const int N=200005;
int n,val[N*2],son[N*2][2],tot=0,rt;
int root[N*2],ls[N*2*20],rs[N*2*20];
LL sum[N*2*20];
void dfsread(int &x){
x=++tot;
scanf("%d",&val[x]);
if (!val[x]){
dfsread(son[x][0]);
dfsread(son[x][1]);
}
}
void pushup(int rt){
sum[rt]=sum[ls[rt]]+sum[rs[rt]];
}
LL sumL,sumR;
void add(int &rt,int L,int R,int v){
rt=++tot;
int mid=(L+R)>>1;
if (L==R){
sum[rt]=1;
return;
}
if (v<=mid)
add(ls[rt],L,mid,v);
else
add(rs[rt],mid+1,R,v);
pushup(rt);
}
int merge(int a,int b){
if (!a||!b)
return a+b;
sumL+=sum[rs[a]]*sum[ls[b]];
sumR+=sum[ls[a]]*sum[rs[b]];
ls[a]=merge(ls[a],ls[b]);
rs[a]=merge(rs[a],rs[b]);
pushup(a);
return a;
}
LL dfs(int x){
LL ans=0;
if (!val[x]){
ans=dfs(son[x][0])+dfs(son[x][1]);
sumL=sumR=0;
root[x]=merge(root[son[x][0]],root[son[x][1]]);
ans+=min(sumL,sumR);
}
else
add(root[x],1,n,val[x]);
return ans;
}
int main(){
scanf("%d",&n);
dfsread(rt);
printf("%lld",dfs(rt));
return 0;
}

  

BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对的更多相关文章

  1. bzoj2212[Poi2011]Tree Rotations [线段树合并]

    题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...

  2. 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并

    [BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...

  3. bzoj2212/3702 [Poi2011]Tree Rotations 线段树合并

    Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some in ...

  4. BZOJ.2212.[POI2011]Tree Rotations(线段树合并)

    题目链接 \(Description\) 给定一棵n个叶子的二叉树,每个叶节点有权值(1<=ai<=n).可以任意的交换两棵子树.问最后顺序遍历树得到的叶子权值序列中,最少的逆序对数是多少 ...

  5. Bzoj P2212 [Poi2011]Tree Rotations | 线段树合并

    题目链接 通过观察与思考,我们可以发现,交换一个结点的两棵子树,只对这两棵子树内的节点的逆序对个数有影响,对这两棵子树以外的节点是没有影响的.嗯,然后呢?(っ•̀ω•́)っ 然后,我们就可以对于每一个 ...

  6. BZOJ_2212_[Poi2011]Tree Rotations_线段树合并

    BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...

  7. [POI2011]ROT-Tree Rotations 线段树合并|主席树 / 逆序对

    题目[POI2011]ROT-Tree Rotations [Description] 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有\(n\)个叶子节点,满足这些权值为 ...

  8. [bzoj2212]Tree Rotations(线段树合并)

    解题关键:线段树合并模板题.线段树合并的题目一般都是权值线段树,因为结构相同,求逆序对时,遍历权值线段树的过程就是遍历所有mid的过程,所有能求出所有逆序对. #include<iostream ...

  9. BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )

    线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...

随机推荐

  1. Spring Cloud构建微服务架构(五)服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: 我们使用Spring Cloud Netflix中的Eureka实现了服务 ...

  2. Codeforces 1097G Vladislav and a Great Legend [树形DP,斯特林数]

    洛谷 Codeforces 这题真是妙的很. 通过看题解,终于知道了\(\sum_n f(n)^k​\)这种东西怎么算. update:经过思考,我对这题有了更深的理解,现将更新内容放在原题解下方. ...

  3. ACM-ICPC 2018 焦作赛区网络预赛 I Save the Room

    Bob is a sorcerer. He lives in a cuboid room which has a length of AAA, a width of BBB and a height ...

  4. springboot第一个项目【创建】

    1.new project,不勾选create from archetype,直接选择 2.next下一步 在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个属性构成的,分别是groupId ...

  5. Confluence 6 Windows 中以服务方式自动重启的原因

    针对长时间使用的 Confluence,我们推荐你配置 Confluence 自动随操作系统重启而启动.针对一些 Windows 的服务器,这意味着需要让 Confluence 以服务的方式运行. 有 ...

  6. Confluence 6 PostgreSQL 测试你的数据库连接

    在你的数据库设置界面,有一个 测试连接(Test connection)按钮可以检查: Confluence 可以连接你的数据库服务器 数据库字符集的编码是否设置正确 你的数据库用户是否有正确的权限可 ...

  7. 初见TensorFlow :知其所以然

    2.1 TensorFlow的主要依赖包 TensorFlow依赖的两个最主要的工具包——Protocol Buffer和Bazel. 2.1.1 Protocol Buffer Protocol B ...

  8. LeetCode(97):交错字符串

    Hard! 题目描述: 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = &qu ...

  9. Fiddler抓包6-get请求(url详解)

    前言 上一篇介绍了Composer的功能,可以模拟get和post请求,get请求有些是不带参数的,这种比较容易,直接放到url地址栏就行.有些get请求会带有参数,本篇详细介绍url地址格式. 一. ...

  10. HTML5 CSS3 Transform 笔记 (scale不起作用)

    Transform的 scale属性不能作用于 inline元素上,例如span 并且动画 animation  也不能作用于inline元素上 可以给span加display:inline-bloc ...