题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212

思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换.

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 4e5+;
int n,cnt,idx;
ll ans,cnt1,cnt2;
int v[M],l[M],r[M],root[M];
int sum[M*],ls[M*],rs[M*];
void init_tree(int x){
scanf("%d",&v[x]);
if(!v[x]){
l[x] = ++cnt;
init_tree(l[x]);
r[x] = ++cnt;
init_tree(r[x]);
}
} void pushup(int rt){
sum[rt] = sum[ls[rt]] + sum[rs[rt]];
} void build(int p,int l,int r,int &rt){
if(!rt) rt = ++idx;
if(l == r){
sum[rt] = ;
return ;
}
int mid = (l + r) >> ;
if(p <= mid) build(p,l,mid,ls[rt]);
else build(p,mid+,r,rs[rt]);
pushup(rt);
} 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);
cnt = ;
init_tree();
for(int i = ;i <= cnt;i ++){
if(v[i])
build(v[i],,n,root[i]);
}
solve();
printf("%lld\n",ans);
return ;
}

bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. BZOJ2212 [Poi2011]Tree Rotations 线段树合并 逆序对

    原文链接http://www.cnblogs.com/zhouzhendong/p/8079786.html 题目传送门 - BZOJ2212 题意概括 给一棵n(1≤n≤200000个叶子的二叉树, ...

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

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

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

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

  9. [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】

    题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...

随机推荐

  1. Android中AsyncTask的使用

    原文 https://blog.csdn.net/liuhe688/article/details/6532519 在Android中实现异步任务机制有两种方式,Handler和AsyncTask. ...

  2. Angularjs实现select的下拉列表

    练习使用angularjs实现一个select下拉列表: <div ng-app="selectApp" ng-controller="selectControll ...

  3. c# 获取文件本身的哈希值

    1. 哈希值是什么 我个人认为,哈希值是一个统称,也就是经过加密算法后得出的长度较短.位数固定的输出序列即散列值,这个哈希值是一个凭证,一个数字签名之类的,唯一对应你加密之前的东西,这都是我自个儿觉得 ...

  4. Linux查看特定端口是否被占用并kill掉相关进程

    今天在搭建Zookeeper集群的时候,需要频繁启动zookeeper,但是启动的时候,有时会提示下列错误信息: zookeeper需要的地址已经被占用了,其实是因为上一次的zookeeper没有关闭 ...

  5. EZ 2018 05 26 NOIP2018 模拟赛(十六)

    这次难道就是传说中的标准分大赛?而且这次比赛的链接不翼而飞了 一堆人153pts然后就有Rank4?看来这个Rank4不值钱了,才涨了50+的Rating. 不过还好最后5min的时候想出了T1正解, ...

  6. BugkuCTF 计算器

    前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...

  7. LDAP学习笔记总结

    一.LDAP概念LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根 ...

  8. zabbix监控交换机、防火墙等网络设备

    zabbix3.4.4监控交换机/防火墙是非简单,只需知道交换机/防火墙的snmp密码,然后连接下自带或导入的snmp模板,就可以完成监控了.比如添加地址为172.10.11.5的交换机监控. 1)登 ...

  9. [linux] VirtualBox复制虚拟机

    环境: Oracle VM VirtualBox 5.0.20 CentOS-6.7-x86_64-minimal.iso 1.复制虚拟机 -->右击休眠状态模板虚拟机,选择复制 -->填 ...

  10. C-数据结构-typedef的用法

    .typedef的用法 # include <stdio.h> typedef int zhang; //为数据类为int从新取名为zhang 等价于int typedef struct ...