[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. malloc 函数详解【转】

    转自:https://www.cnblogs.com/Commence/p/5785912.html 很多学过C的人对malloc都不是很了解,知道使用malloc要加头文件,知道malloc是分配一 ...

  2. CSS选择器中带点(.)怎么办?

    在SharePoint中很多元素的ID都用点(.)来连接的,比如: <li class="ms-cui-group" id="Ribbon.Documents.Ed ...

  3. unbuntu中如何像Windows一样顺畅的切换中英文输入法

    1.首先在unbuntu安装搜狗拼音输入法(这个不用教了) 2.点击右上角的搜狗拼音的图标点击设置进入设置页面 3.选择高级 4.选择Fcitx设置 5.添加输入法英语(美国) 6.在设置中选择按键, ...

  4. Go语言规格说明书 之 结构体类型(Struct types)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...

  5. Unicode范围预览

    链接: https://www.zhangxinxu.com/study/201611/show-character-by-charcode.php?range=4E00-9FBB 备注: Unico ...

  6. iOS 中的Certificate,Provisioning Profile 的一些注意 (不断完善中)

    注册apple id 有1年多了,这些概念还是模模糊糊的,决定在这里总结一下. 请参阅官方文档 App Distribution Guide code singing的作用如下: Code signi ...

  7. 区间dp的一些模式和总结

    参考博客:https://blog.csdn.net/my_sunshine26/article/details/77141398 https://blog.csdn.net/qq_38569113/ ...

  8. 我靠,上班eclipse看糗事百科

    package test; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; ...

  9. 《剑指offer》-斐波那契数列

    大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. n<=39 这么直接的问fibonacci,显然是迭代计算.递归的问题在于重复计算,而迭代则避免了这一点:递归是自 ...

  10. Javascript中的反射机制(五)

    一: 什么是反射机制 反射机制指的是程序在运行时能够获取自身的信息.例如一个对象能够在运行时知道自己有哪些方法和属性. 二: 在JavaScript中利用for(…in…)语句实现反射 在JavaSc ...