题解:

每一次合并两个对

修改操作就和普通的堆一样

代码:

#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int val[N],n,m,rt[N],tot,dist[N],c[N][],a[N],x,y,fa[N],size[N];
int merge(int x,int y)
{
if (!x||!y)return x+y;
if (val[x]<val[y])swap(x,y);
c[x][]=merge(c[x][],y);
size[x]=size[c[x][]]+size[c[x][]]+;
fa[c[x][]]=x;
if (dist[c[x][]]<dist[c[x][]])swap(c[x][],c[x][]);
dist[x]=dist[c[x][]]+;
return x;
}
int top(int x)
{
for (;fa[x]!=x;x=fa[x]);
return x;
}
int newnode(int x)
{
val[++tot]=x;
size[tot]=;fa[tot]=tot;
c[tot][]=c[tot][]=dist[tot]=;
return tot;
}
void down(int x)
{
int i=x;
if (c[x][]&&val[i]<val[c[x][]])i=c[x][];
if (c[x][]&&val[i]<val[c[x][]])i=c[x][];
if (x!=i)
{
swap(val[x],val[i]);
down(i);
}
}
int main()
{
while (~scanf("%d",&n))
{
memset(val,,sizeof val);
memset(c,,sizeof c);
memset(rt,,sizeof rt);
memset(fa,,sizeof fa);
tot=;
for (int i=;i<=n;i++)scanf("%d",&a[i]);
for (int i=;i<=n;i++)rt[i]=newnode(a[i]);
scanf("%d",&m);
while (m--)
{
scanf("%d%d",&x,&y);
int tx=top(x),ty=top(y);
if (tx==ty)puts("-1");
else
{
val[tx]/=;down(tx);
val[ty]/=;down(ty);
rt[tx]=merge(tx,ty);rt[ty]=;
printf("%d\n",val[rt[tx]]);
}
}
}
}

hdu1512的更多相关文章

  1. HDU1512 ZOJ2334 Monkey King 左偏树

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

  2. hdu1512 Monkey King

    Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each ...

  3. [HDU1512/ZOJ2334]Monkey King-左偏树-可合并堆

    Problem Monkey King Solution 本题是裸的左偏树,一个模板就可以过了.对于每个操作对节点先删除/2再合并. 注意本题在HDU上评测特别坑,是多组数据,而且经常出现MLE的情况 ...

  4. hdu1512 Monkey King(左偏树 + 并查集)

    Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...

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

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

  6. 左偏树自己的一点理解【hdu1512】【Monkey King】

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34693563 向大(hei)佬(e)势力学(di ...

  7. hdu1512 Monkey King(并查集,左偏堆)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1512 题目大意:有n个猴子,一开始每个猴子只认识自己.每个猴子有一个力量值,力量值越大表示这个猴子打架 ...

  8. 帝都Day4(3)——还是数据结构

    可并堆 左偏树中 dist[x]=dist[rs[x]]+1 合并的时候,把权志较大的根作为根节点,把这棵树右子树和另一棵树合并. 说明白点:(上图描述有点问题) 设x表示根权值较大的左偏树,y表示根 ...

  9. 左偏树 / 非旋转treap学习笔记

    背景 非旋转treap真的好久没有用过了... 左偏树由于之前学的时候没有写学习笔记, 学得也并不牢固. 所以打算写这么一篇学习笔记, 讲讲左偏树和非旋转treap. 左偏树 定义 左偏树(Lefti ...

随机推荐

  1. mysql 数据操作 单表查询 where约束 between and or

    WHERE约束 where字句中可以使用: 比较运算符:>< >=  <=  != between 80 and 100 值在80到100之间   >=80  <= ...

  2. [.Net]System.OutOfMemoryException异常

    1. 一个异常情景 加载15000条等高线,平均每条线有400个点到三维球上,等待时间太长.而且可能会报内存异常. 2. 不错的分析 http://wenku.baidu.com/view/14471 ...

  3. Flume+Morphlines实现数据的实时ETL

    转载:http://mp.weixin.qq.com/s/xCSdkQo1XMQwU91lch29Uw Apache Flume介绍: Apache Flume是一个Apache的开源项目,是一个分布 ...

  4. NodeJS学习笔记六

    Symbol简介 ES6引入了一种新的原始数据类型Symbol,表示独一无二的值.它是JavaScript语言的第七种数据类型,前六种是:Undefined.Null.布尔值(Boolean).字符串 ...

  5. Vector、List、LinkedList

    Vector 遍历 Vector<String> vestor =new Vector<String>(); vestor.add("qq"); vesto ...

  6. js 中的[] {}是什么意思

    <div id="aa1">aaaaa</div><div id="aa2">bbbb</div><div ...

  7. poj1177 Picture 矩形周长并

    地址:http://poj.org/problem?id=1177 题目: Picture Time Limit: 2000MS   Memory Limit: 10000K Total Submis ...

  8. Entity Framework With Oracle(转)

    虽然EF6都快要出来了,但是对于Oracle数据库,仍然只能用DB first和Model First来编程,不能用Code First真是一个很大的遗憾啊. 好了,废话少说,我们来看看EF中是如何用 ...

  9. 梅尔频率倒谱系数(MFCC) 学习笔记

    最近学习音乐自动标注的过程中,看到了有关使用MFCC提取音频特征的内容,特地在网上找到资料,学习了一下相关内容.此笔记大部分内容摘自博文 http://blog.csdn.net/zouxy09/ar ...

  10. OpenCV图像增强算法实现(直方图均衡化、拉普拉斯、Log、Gamma)

    http://blog.csdn.net/dcrmg/article/details/53677739 1. 基于直方图均衡化的图像增强   直方图均衡化是通过调整图像的灰阶分布,使得在0~255灰阶 ...