[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. 开源api文档

    蒲公英——API文档 https://www.pgyer.com/doc/api

  2. 聊聊、Git 常用命令

    创建本地仓库git initgit add .git commit -m "xxxxx"git remote add origin http://git.xxx.com/xxx.g ...

  3. PAT——甲级1042:Shuffling Mashine

    终于做到甲级了 就一个感觉....题目是真的看不懂,亏我还是四六级都过了的人....可是看完题愣是一点都不懂是什么意思. 1042 Shuffling Machine (20 point(s)) Sh ...

  4. 网络--路由表&IP选路

    路由表的 flags 字段显示路由状态: A 活动的休眠网关检测在路由上被启用.本字段只适用于 AIX 5.1 或更新版本. U :Up. H :路由至主机而不是网络. G :路由至网关. 不带G表示 ...

  5. 重复造轮子系列--内存池(C语言)

    这个代码是我上个公司工作项目的里面内存管理(基于伙伴算法)的一个简化又简化的版本. 因为没有内存边界检查: 因为没有内存使用统计: 因为没有考虑线程安全: 因为没有内存分配操作的具体文件位置信息: 因 ...

  6. NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet

    springMVC 内嵌jetty时,出现了NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet: 添加了 ...

  7. Batting Practice LightOJ - 1408

    Batting Practice LightOJ - 1408(概率dp) 题意:有无限个球,进球的概率为p,问你连续不进k1个球或者连续进k2个球需要使用的球的个数的期望 思路: \(定义f[i]表 ...

  8. BZOJ1176 [Balkan2007]Mokia 【CDQ分治】

    题目 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. 输入格式 ...

  9. CF992E Nastya and King-Shamans 解题报告

    CF992E Nastya and King-Shamans 题意翻译 给定一个序列 \(a_i\),记其前缀和序列为 \(s_i\),有 \(q\) 个询问,每次单点修改,询问是否存在一个 \(i\ ...

  10. oracle修改数据遇到的坑

    select t.*,mt.*,mr.rowid,mr.* from manu_routecardlist mr left join manu_routecard t on t.routecard_i ...