[POI2011]Rotacje na drzewie (2)

题目大意:

一棵有\(n\)个叶子结点的二叉树,每个叶子结点有一个权值,恰好是\(1\sim n\)的一个排列,你可以任意交换每一对子结点,使得从左往右的权值序列中,逆序对数量最少,求最少逆序对数。

原题:\(n\le2\times10^5\),空间限制64MB;

加强版:\(n\le10^6\),空间限制128MB。

思路:

原题可以直接使用线段树合并,注意空间回收即可通过此题。

加强版可以使用pb_ds红黑树按秩和并。

源代码1:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
int n;
int64 cnt0,cnt1,ans;
struct Node {
Node *left,*right;
int sum;
Node():left(NULL),right(NULL),sum(0) {}
};
inline int count(const Node *const &p) {
return p?p->sum:0;
}
Node *build(const int &b,const int &e,const int &x) {
Node *p=new Node;
p->sum++;
if(b==e) return p;
const int mid=(b+e)>>1;
if(x<=mid) p->left=build(b,mid,x);
if(x>mid) p->right=build(mid+1,e,x);
return p;
}
Node *merge(Node *p,Node *q,const int &b,const int &e) {
if(!p||!q) return p?:q;
p->sum+=q->sum;
if(b==e) {
delete q;
return p;
}
const int mid=(b+e)>>1;
cnt0+=(int64)count(p->left)*count(q->right);
cnt1+=(int64)count(q->left)*count(p->right);
p->left=merge(p->left,q->left,b,mid);
p->right=merge(p->right,q->right,mid+1,n);
delete q;
return p;
}
Node* solve() {
const int w=getint();
if(w!=0) {
return build(1,n,w);
} else {
Node *p=merge(solve(),solve(),1,n);
ans+=std::min(cnt0,cnt1);
cnt0=cnt1=0;
return p;
}
}
int main() {
n=getint();
solve();
printf("%lld\n",ans);
return 0;
}

源代码2:

#include<cstdio>
#include<cctype>
#include<functional>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
typedef __gnu_pbds::tree<int,__gnu_pbds::null_type,std::less<int>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update> rbt;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
typedef unsigned long long uint64;
int n;
int64 ans;
inline rbt *merge(rbt *p,rbt *q) {
if(p->size()<q->size()) std::swap(p,q);
uint64 tmp=0;
for(int x:*q) tmp+=p->order_of_key(x);
ans+=std::min(1ll*p->size()*q->size()-tmp,tmp);
for(int x:*q) p->insert(x);
delete q;
return p;
}
rbt *solve() {
const int w=getint();
if(w!=0) {
rbt *p=new rbt;
p->insert(w);
return p;
} else {
return merge(solve(),solve());
}
}
int main() {
n=getint();
solve();
printf("%lld\n",ans);
return 0;
}

[POI2011]Rotacje na drzewie (2)/[BZOJ3702]二叉树的更多相关文章

  1. bzoj3702二叉树 线段树合并

    3702: 二叉树 Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 600  Solved: 272[Submit][Status][Discuss] ...

  2. [bzoj3702] 二叉树

    一个节点的儿子是否交换,不会影响到它和兄弟节点间的逆序对数. 所以每次合并线段树的时候算一下交换与不交换的逆序对数,然后选个较小值就行了. #include<cstdio> #includ ...

  3. [bzoj3702/2212][Poi2011]二叉树/Tree Rotations_线段树

    二叉树 Tree Rotations bzoj-3702 bzoj-2212 Poi-2011 题目大意:现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有n个叶子节点,满足 ...

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

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

  5. 二叉树hdu1710

    学习二叉树,看了两天也不明白,唉!acm之路让我体验到要付出巨大的努力,废话不多说,看我网上找到的代码: 此题题意很明确,给你先序遍历,中序遍历,求后序遍历.但代码就让我找不到东西了. http:// ...

  6. BZOJ2212: [Poi2011]Tree Rotations

    2212: [Poi2011]Tree Rotations Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 391  Solved: 127[Submi ...

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

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

  8. python实战--数据结构二叉树

    此文将讲述如何用python实战解决二叉树实验 前面已经讲述了python语言的基本用法,现在让我们实战一下具体明确python的用法 点击我进入python速成笔记 先看一下最终效果图: 首先我们要 ...

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

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

随机推荐

  1. 再谈:自定义结构体的对齐问题之__attribute__ ((packed))方法【转】

    转自:https://blog.csdn.net/ipromiseu/article/details/5955295 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.c ...

  2. vux安装中遇到的坑

    1.输入 npm install vux --save 2.输入 npm install vux-loader --save-dev(没安装的时候,会一直报错) 3.build/webpack.bas ...

  3. [转]解决win7 64位操作系统下安装PL/SQL后连接报错问题: make sure you have the 32 bits oracle client installed

    1. 在Oracle官网(http://www.oracle.com/technetwork/topics/winsoft-085727.html)下载文件: instantclient-basic- ...

  4. centos7 部署 open-falcon 0.2.1

    =============================================== 2019/4/28_第1次修改                       ccb_warlock 更新 ...

  5. Day5-----------------------系统监控

    1.top 命令 查看终端信息 who 显示终端用户有哪些 bash 开启终端进程 PID:进程身份证 buffer:缓冲区 cache:高速缓存 进程:动起来的文件,CPU调用运行的过程 2.fre ...

  6. JUnit单元测试--IntelliJ IDEA

    单元测试的基本使用 一.环境配置 使用idea IDE 进行单元测试,首先需要安装JUnit 插件. 1.安装JUnit插件步骤 File-->settings-->Plguins--&g ...

  7. 关于z-index的那些事儿

    关于z-index的真正问题是,很少有人理解它到底是怎么用.其实它并不复杂,但是如果你从来没有花一定时间去看具体的z-index相关文档,那么你很可能会忽略一些重要的信息. 不相信我吗?好吧,看看你能 ...

  8. java多线程快速入门(十二)

    在静态方法上面加synchonizd用的是字节码文件锁 package com.cppdy; class MyThread8 implements Runnable { private static ...

  9. 性能测试三十二:监控之Java线程监控

    线程的五种状态 * 新建:new * 运行:runnable * 等待:waitting(无限期等待),timed waitting(限期等待) * 阻塞:blocked * 结束:terminate ...

  10. ROS----TUT-RIM协作机器人和Actin-ROS接口

    TUT-RIM: Collaborative Intelligent Heavy Machinery and Robotics http://www.ros.org/news/2017/02/tutr ...