3702: 二叉树

Time Limit: 15 Sec  Memory Limit: 256 MB
Submit: 600  Solved: 272
[Submit][Status][Discuss]

Description

现在有一棵二叉树,所有非叶子节点都有两个孩子。在每个叶子节点上有一个权值(有n个叶子节点,满足这些权值为1..n的一个排列)。可以任意交换每个非叶子节点的左右孩子。
要求进行一系列交换,使得最终所有叶子节点的权值按照中序遍历写出来,逆序对个数最少。

Input

第一行n
下面每行,一个数x
如果x==0,表示这个节点非叶子节点,递归地向下读入其左孩子和右孩子的信息,
如果x!=0,表示这个节点是叶子节点,权值为x。

Output

一行,最少逆序对个数。

Sample Input

3
0
0
3
1
2

Sample Output

1

HINT

对于100%的数据:2<=n<=200000。

两棵子树交换不影响子树内的值,所以每次只考虑相邻两棵子树怎样放,再考虑上一层怎么放

线段树合并: 开权值线段记录原树中每个节点的子树对应拥有的值,向上合并的时候统计答案

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
#define N 200050
using namespace std;
int n,tot,cnt,v[N*3],rt[N*3],l[N*3],r[N*3],ls[N*20],rs[N*20],sum[N*20];
ll ans,a,b;
void tree(int &x){
x=++tot;
scanf("%d",&v[tot]);
if(v[tot])return;
tree(l[x]);
tree(r[x]);
}
void update(int &u,int l,int r,int p){
if(!u)u=++cnt;sum[u]++;
if(l==r)return;
int mid=l+r>>1;
if(p<=mid)update(ls[u],l,mid,p);
else update(rs[u],mid+1,r,p);
}
int merge(int x,int y){
if(!x)return y;
if(!y)return x;
a+=1ll*sum[ls[x]]*sum[rs[y]];
b+=1ll*sum[rs[x]]*sum[ls[y]];
ls[x]=merge(ls[x],ls[y]);
rs[x]=merge(rs[x],rs[y]);
sum[x]=sum[ls[x]]+sum[rs[x]];
return x;
} void solve(int x){
if(!x)return;
solve(l[x]);solve(r[x]);
if(v[x])return;
a=0;b=0;
rt[x]=merge(rt[l[x]],rt[r[x]]);
ans+=min(a,b);
} int main(){
scanf("%d",&n);
int root;tree(root);
for(int i=1;i<=tot;i++)
if(v[i])update(rt[i],1,n,v[i]);
solve(1);cout<<ans;
return 0;
}

bzoj3702二叉树 线段树合并的更多相关文章

  1. bzoj3702/bzoj2212 二叉树 (线段树合并)

    用线段树记每个子树中包含的数,然后合并的时候算出来逆序对的数量(合并a,b时,就是size[ch[a][1]]*size[ch[b][0]]),来决定这个子树要不要翻转 #include<bit ...

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

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

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

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

  4. hdu6133 Army Formations 线段树合并

    给你一棵有n个节点的二叉树,每个节点有一个权值,对于一棵子树u,将u的子树中的节点权值从大到小排序,令sz[u]为子树u的大小, 则ans[u] = 1 * a[1] + 2 * a[2] + ... ...

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

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

  6. 2018.07.07 BZOJ2212: Poi2011Tree Rotations(线段树合并)

    2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MB Description Byteasar the garde ...

  7. 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]

    题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...

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

    题目链接 BZOJ2212 题解 一棵子树内的顺序不影响其与其它子树合并时的答案,这一点与归并排序的思想非常相似 所以我们只需单独处理每个节点的两棵子树所产生的最少逆序对即可 只有两种情况,要么正序要 ...

  9. 2017多校第8场 HDU 6133 Army Formations 线段树合并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6133 题意:给你一棵n个节点的二叉树,每个节点有一个提交任务的时间,每个节点总的提交任务的罚时为:提交 ...

随机推荐

  1. 微信支付 chooseWXPay:fail

    本来以为解决了微信支付get_brand_wcpay_request:faill这个问题后就万事大吉了,结果又迈入了另一个坑... 问题原因: 1.生成签名的时间戳参数名timestamp的s大小写问 ...

  2. 如何书写高效的css样式

    如何书写高效的css样式? 有以下四个关键要素: 1.高效的css 2.可维护的css 3.组件化的css 4.hack-free  css 书写高效的css: 1.使用外联样式替代行间样式或内嵌样式 ...

  3. Mongodb 3 查询优化(慢查询Profiling)

    开启慢查询Profiling Profiling级别说明 0:关闭,不收集任何数据. 1:收集慢查询数据,默认是100毫秒. 2:收集所有数据 1.通过修改配置文件开启Profiling 修改启动mo ...

  4. Xamarin控件使用之ListView

    listview单列多行的显示,以后再加多列多行的实例. [Activity(Label = "GraphicAll", LaunchMode = LaunchMode.Singl ...

  5. LeetCode & Q169-Majority Element-Easy

    Array Divide and Conquer Bit Manipulation Description: Given an array of size n, find the majority e ...

  6. Python内置函数(41)——id

    英文文档: id(object) Return the "identity" of an object. This is an integer which is guarantee ...

  7. Oracle10g物理DG详细配置方法及步骤

    --测试环境:    OS:Redhat linux(64)    Primary:    IP:192.168.94.198    SID:dgdb1    Hostname:dg1    DB_U ...

  8. 操作MP3文件的元数据

    参见:http://jingyan.baidu.com/article/03b2f78c4d5eae5ea237aee7.html 一.MP3文件的元数据 一个规则的MP3文件大致含有3个部分: TA ...

  9. Spring Security 入门(1-3-2)Spring Security - http元素 - intercept-url配置

    http元素下可以配置登录页面,也可以配置 url 拦截. 1.直接配置拦截url和对应的访问权限 <security:http use-expressions="false" ...

  10. Python基础数据类型之int、bool、str

    数据类型:int  bool  str  list  元祖  dict  集合 int:整数型,用于各种数学运算. bool:只有两种,True和False,用户判断. str:存储少量数据,进行操作 ...