[POI2011]Rotacje na drzewie (2)/[BZOJ3702]二叉树
[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]二叉树的更多相关文章
- bzoj3702二叉树 线段树合并
3702: 二叉树 Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 600 Solved: 272[Submit][Status][Discuss] ...
- [bzoj3702] 二叉树
一个节点的儿子是否交换,不会影响到它和兄弟节点间的逆序对数. 所以每次合并线段树的时候算一下交换与不交换的逆序对数,然后选个较小值就行了. #include<cstdio> #includ ...
- [bzoj3702/2212][Poi2011]二叉树/Tree Rotations_线段树
二叉树 Tree Rotations bzoj-3702 bzoj-2212 Poi-2011 题目大意:现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有n个叶子节点,满足 ...
- bzoj3702/bzoj2212 二叉树 (线段树合并)
用线段树记每个子树中包含的数,然后合并的时候算出来逆序对的数量(合并a,b时,就是size[ch[a][1]]*size[ch[b][0]]),来决定这个子树要不要翻转 #include<bit ...
- 二叉树hdu1710
学习二叉树,看了两天也不明白,唉!acm之路让我体验到要付出巨大的努力,废话不多说,看我网上找到的代码: 此题题意很明确,给你先序遍历,中序遍历,求后序遍历.但代码就让我找不到东西了. http:// ...
- BZOJ2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 391 Solved: 127[Submi ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- python实战--数据结构二叉树
此文将讲述如何用python实战解决二叉树实验 前面已经讲述了python语言的基本用法,现在让我们实战一下具体明确python的用法 点击我进入python速成笔记 先看一下最终效果图: 首先我们要 ...
- BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...
随机推荐
- java 基础(转自索宁)
一.方法函数 函数也称为方法,就是定义在类中的具有特定功能的一段独立代码.用于定义功能,提高代码的复用性. 函数的特点1> 定义函数可以将功能代码进行封装,便于对该功能进行复用:2> 函数 ...
- 使用FreeSWITCH做电话自动回访设置
一.背景介绍: 目前公司在处理客户回访方面,需要人工进行电话回访,尤其是逢年过节的时候,电话问候更能体现服务的品质: 在某些公司,电话销售员需要给大批量的陌生用户打电话,如果能过滤掉不关心的用户,销售 ...
- Ex 6_23 一个生产系统共包含n个顺序执行的阶段..._第七次作业
- Python-html css 盒模型
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>ht ...
- java I/O系统 LineNumberReader类
LineNumbeReader类可以很方便的读取文件的行号 package ch13; import java.io.*; import io.BufferedInputFile; public cl ...
- java lambda 的用法
一.打印数组 String[] s = "fdsfsdfds".split(""); Stream<String> str = Stream.of( ...
- ural1855 线段树区间更新+推公式维护一元二次式
和威威猫系列故事差不多,都是根据条件推出公式 /* 操作c a b d:a到b道路上的所有边权值加d 操作e a b:问a到b中包含的道路的平均权值 区间平均值=所有可能路径权值/所有路径数, 而路径 ...
- Action属性接收参数
一.action的属性(地址栏传参)接收参数:如果使用的JDK属性不一致,则会使得传值无法实现.解决办法:1.系统自身需要用到的JDK(window——>属性——>Java——>In ...
- hdu 2157 从a点走到b点刚好k步的方案数是多少 (矩阵快速幂)
n个点 m条路 询问T次 从a点走到b点刚好k步的方案数是多少 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值把 给定的图转为邻接矩阵,即A(i,j)=1当且仅当存 ...
- spring配置redis注解缓存
前几天在spring整合Redis的时候使用了手动的方式,也就是可以手动的向redis添加缓存与清除缓存,参考:http://www.cnblogs.com/qlqwjy/p/8562703.html ...