[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34693563

向大(hei)佬(e)势力学(di)习(tou)

前段时间学的左偏树,今天复习了一遍,写篇博客加深印象。

左偏树是可合并堆中好写好理解的数据结构。其定义为:

1、满足堆的性质

2、节点的左子节点的距离不小于右子节点的距离

3、每一个子树也满足左偏树的性质

如图

然后这个“距离”懵逼了我好久。标准的定义是:

到最近的右儿子为空的节点的距离

可是单是知道定义,到底该怎么处理呢?直观感觉难查询难计算。那么,我们将其维护成某些特殊的状态:一直向右就是右子节点,就方便计算和维护了,于是还多了一条性质:”节点的距离等于它的右子节点的距离加1“,这个性质对于维护来说即为重要。

这里有一个小技巧:把空节点的dis设为-1,这样在维护的时候就不用分情况讨论了。

a[x].dis=a[a[x].rs].dis+1;

当在合并时,可能会出现左子节点距离小于右子节点距离的情况,这时只需要将左右儿子交换即可

if(a[a[x].ls].dis<a[a[x].rs].dis) swap(a[x].ls,a[x].rs);

现在重头戏来了,如何合并?

借用某大大的图解,我也是看了之后才理解到的

http://www.cnblogs.com/yc_sunniwell/archive/2010/06/28/1766756.html







由图可以看出,合并过程其实更像是“插入”过程,是将一棵树插入另一棵树。一直向右边走,以堆的性质为条件判断是谁插入谁。

由于维护了左偏的性质,dis最多就log层,所以时间上是不会爆的。

下面是最好模板化的代码

int merge(int x,int y){
if(x==0) return y;
if(y==0) return x;
if(a[x].key<a[y].key){
swap(x,y);
}
a[x].rs=merge(a[x].rs,y);
if(a[a[x].ls].dis<a[a[x].rs].dis) swap(a[x].ls,a[x].rs);
a[x].dis=a[a[x].rs].dis+1;
return x;
}

插入和删除就很好写的,只不过是对merge进行一些花样操作

插入一个点,可以把这个点看做一棵树,merge就好了

删除其实就是把左右子树合并起来

好了,基础操作总结完了

然而左偏树常常与并查集连用,我认为把祖先指向堆顶元素会方便一些,但在处理时还是有点绕

下面由一道入门题结束吧

Problem Description

Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can’t avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.

Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).

And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.

Input

There are several test cases, and each case consists of two parts.

First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).

Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.

Output

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.

Sample Input

5

20

16

10

10

4

5

2 3

3 4

3 5

4 5

1 5

Sample Output

8

5

5

-1

10

题目大意

有n只猴子,每只猴子有厉害值,一开始素不相识。

两只不熟的猴子相遇,它们会发生争执。然后,它们会邀请它们认识的最厉害的猴子决斗。决斗完这两只决斗的猴子的厉害值都会减半。决斗能促进友谊,这样这两拨素不相识的猴子就都认识了对方。

如果一只猴子不认识任何其他猴子,那么它就会亲自上阵。

每次给出两只猴子x,y,判断它们是否认识对方。若不认识,输出决斗后它们共同所在猴子群体中最厉害猴子的厉害值。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N=100000+5; struct Node {
int ls,rs;
int dis,key;
}a[N];
int fa[N],str[N],n,m; int getfa(int x){
if(fa[x]==x) return x;
return fa[x]=getfa(fa[x]);
}
int merge(int x,int y){
if(x==0) return y;
if(y==0) return x;
if(a[x].key<a[y].key){
swap(x,y);
}
a[x].rs=merge(a[x].rs,y);
if(a[a[x].ls].dis<a[a[x].rs].dis) swap(a[x].ls,a[x].rs);
a[x].dis=a[a[x].rs].dis+1;
return x;
}
int main(){
a[0].ls=a[0].rs=0;
a[0].dis=a[0].key=-1; while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++){
scanf("%d",&str[i]);
fa[i]=i;
a[i].dis=0,a[i].key=str[i];
a[i].ls=a[i].rs=0;
}
scanf("%d",&m);
while(m--){
int aa,bb;
scanf("%d%d",&aa,&bb);
int faa=getfa(aa),fab=getfa(bb);
if(faa==fab){
printf("-1\n");continue;
}
a[faa].key/=2,a[fab].key/=2;
int tmpa=merge(a[faa].ls,a[faa].rs),tmpb=merge(a[fab].ls,a[fab].rs);
a[faa].ls=a[faa].rs=a[fab].ls=a[fab].rs=0;
fa[faa]=merge(faa,tmpa),fa[fab]=merge(fab,tmpb);
fa[fa[faa]]=fa[faa],fa[fa[fab]]=fa[fab];
faa=getfa(faa),fab=getfa(fab);
int tmp=merge(faa,fab);
fa[faa]=fa[fab]=tmp;
printf("%d\n",a[tmp].key);
}
}
return 0;
}

左偏树自己的一点理解【hdu1512】【Monkey King】的更多相关文章

  1. [洛谷P3261] [JLOI2015]城池攻占(左偏树)

    不得不说,这道题目是真的难,真不愧它的“省选/NOI-”的紫色大火题!!! 花了我晚自习前半节课看题解,写代码,又花了我半节晚自习调代码,真的心态爆炸.基本上改得和题解完全一样了我才过了这道题!真的烦 ...

  2. [洛谷P1552] [APIO2012]派遣(左偏树)

    这道题是我做的左偏树的入门题,奈何还是看了zsy大佬的题解才能过,唉,我太弱了. 左偏树总结 Part 1 理解题目 很显然,通过管理关系的不断连边,最后连出来的肯定是一棵树,那么不难得出,当一个忍者 ...

  3. HDU1512 ZOJ2334 Monkey King 左偏树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...

  4. [HDU1512]Monkey King(左偏树)

    用并查集维护猴子们的关系,强壮值用左偏树维护就行了 Code #include <cstdio> #include <algorithm> #include <cstri ...

  5. P3377 【模板】左偏树(可并堆) 左偏树浅谈

    因为也是昨天刚接触左偏树,从头理解,如有不慎之处,跪请指教. 左偏树: 什 么是(fzy说)左偏树啊? 前置知识: 左偏树中dist:表示到右叶点(就是一直往右下找,最后一个)的距离,特别的,无右节点 ...

  6. bzoj 1455: 罗马游戏 左偏树+并查集

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 668  Solved: 247[Submit][Status] Descriptio ...

  7. luogu【P3377】 【模板】左偏树

    左偏树 顾名思义 向左偏的树 (原题入口) 它有啥子用呢??? 当然是进行堆的合并啦2333普通堆的合并其实是有点慢的(用优先队列的话 只能 一个pop 一个push 来操作 复杂度就是O(n log ...

  8. 『左偏树 Leftist Tree』

    新增一道例题 左偏树 Leftist Tree 这是一个由堆(优先队列)推广而来的神奇数据结构,我们先来了解一下它. 简单的来说,左偏树可以实现一般堆的所有功能,如查询最值,删除堆顶元素,加入新元素等 ...

  9. 浅谈左偏树在OI中的应用

    Preface 可并堆,一个听起来很NB的数据结构,实际上比一般的堆就多了一个合并的操作. 考虑一般的堆合并时,当我们合并时只能暴力把一个堆里的元素一个一个插入另一个堆里,这样复杂度将达到\(\log ...

随机推荐

  1. Linux - Shell - 常用方法 - 备忘录

    $? 上一个指令的返回值 =成功,=失败 dmesg 检测系统开机启动信息 $() 对命令的替换,同`` ${} 对变量的替换,同$var $(()) 对内部内容进行整数运算 i= grep AAA ...

  2. Box布局管理

    创建wx.BoxSizer对象时可以指定布局方向: hbox = wx.BoxSizer(wx.HORIZONTAL) 设置为水平方向 hbox = wx.BoxSizer() 默认就是就是水平方向的 ...

  3. solr集群搭建(复制)

    Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南) 1   什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引 ...

  4. chrome浏览器console拓展用法

    chrome 浏览器console打印 使用CSS美化输出信息 console.log("%cThis will be formatted with large, blue text&quo ...

  5. 为啥shmem不回收 | drop_caches

    内核在哪里禁止对tmpfs中内存页的回收 mem.limit_in_bytes同样会触发shrink_zones过程! shrink_zones是代码中的直接内存回收路径 1.try_to_free_ ...

  6. HTML标签的使用要注意语义化

    语义化标签:你认为用什么标签最能描述这块内容,觉得这样表述更有意义,那么就可以使用这个标签. 现在的浏览器对CSS支持都挺完善的(不包括CSS3),讲究的是结构与表现相分离,结构与行为相分离,一个WE ...

  7. Audio Unit 介绍

    关于 Audio Unit iOS 提供了音频处理插件,支持混音,声音均衡,格式转化,以及用于录音,回放,离线渲染,实时对话的输入输出.可以动态载入和使用这些强大而灵活的插件,在 iOS 应用中这些插 ...

  8. Installing Wine 1.5: configure: error: Cannot build a 32-bit program, you need to install 32-bit development libraries(转载)

    Installing Wine 1.5: configure: error: Cannot build a 32-bit program, you need to install 32-bit dev ...

  9. mysql 逻辑架构(三层)

    1.客户端(主要处理连接,授权认证,安全等). 2.MYSQL服务器层(核心服务功能都在这层,包括,查询解析,分析,优化,缓存以及所有的内置函数,所有跨存储引擎的功能都在这层实现:存储过程,触发器,视 ...

  10. linux 内核库函数 【转】

    转自:http://blog.chinaunix.net/uid-20321537-id-1966892.html 当编写驱动程序时,一般情况下不能使用C标准库的函数.Linux内核也提供了与标准库函 ...