hdu1512
题解:
每一次合并两个对
修改操作就和普通的堆一样
代码:
#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的更多相关文章
- HDU1512 ZOJ2334 Monkey King 左偏树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...
- hdu1512 Monkey King
Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each ...
- [HDU1512/ZOJ2334]Monkey King-左偏树-可合并堆
Problem Monkey King Solution 本题是裸的左偏树,一个模板就可以过了.对于每个操作对节点先删除/2再合并. 注意本题在HDU上评测特别坑,是多组数据,而且经常出现MLE的情况 ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
- [HDU1512]Monkey King(左偏树)
用并查集维护猴子们的关系,强壮值用左偏树维护就行了 Code #include <cstdio> #include <algorithm> #include <cstri ...
- 左偏树自己的一点理解【hdu1512】【Monkey King】
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34693563 向大(hei)佬(e)势力学(di ...
- hdu1512 Monkey King(并查集,左偏堆)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1512 题目大意:有n个猴子,一开始每个猴子只认识自己.每个猴子有一个力量值,力量值越大表示这个猴子打架 ...
- 帝都Day4(3)——还是数据结构
可并堆 左偏树中 dist[x]=dist[rs[x]]+1 合并的时候,把权志较大的根作为根节点,把这棵树右子树和另一棵树合并. 说明白点:(上图描述有点问题) 设x表示根权值较大的左偏树,y表示根 ...
- 左偏树 / 非旋转treap学习笔记
背景 非旋转treap真的好久没有用过了... 左偏树由于之前学的时候没有写学习笔记, 学得也并不牢固. 所以打算写这么一篇学习笔记, 讲讲左偏树和非旋转treap. 左偏树 定义 左偏树(Lefti ...
随机推荐
- nodejs学习笔记Node.js 调试命令
3.4 调试 47 下面是一个简单的例子: $ node debug debug.js < debugger listening on port 5858 connecting ...
- Day22 文件上传下载和javaMail
day22总结 文件上传概述 1 文件上传的作用 例如网络硬盘!就是用来上传下载文件的. 在智联招聘上填写一个完整的简历还需要上传照片呢. 2 文件上传对页面的要求 上传文件的要求比较多,需要 ...
- day08:软件系统的体系结构&Tomcat详解&Web应用&http协议
day08 软件系统体系结构 常见软件系统体系结构B/S.C/S 1.1 C/S C/S结构即客户端/服务器(Client/Server),例如QQ: 需要编写服务器端程序,以及客户端 ...
- 201-React顶级API
一.概述 React是React库的入口点.如果您从<script>标记加载React,则这些顶级API可在React全局中使用.如果你使用npm的ES6,你可以写:import Reac ...
- 运行HBase应用开发程序产生异常,提示信息包含org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory的解决办法
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties Exception in thread ...
- Hive重写表数据丢失风险记录
若在Hive中执行INSERT OVERWRITE重写同一个表的数据时,有可能会造成数据丢失. 如 INSERT OVERWRITE TABLE table_name SELECT * FROM ta ...
- Docker Compose 入门使用指南
Compose is a tool for defining and running multi-container Docker applications. With Compose, you us ...
- R语言apply()函数用法
在R语言的帮助文档里,apply函数的功能是: Retruns a vector or array or list of values obtained by applying a function ...
- 时间格式—My97DatePicker控件使用
一.My97DatePicker控件使用(2步): 1:JSP页面:( class="Wdate" 显示控件图标) 引入控件包中的脚本: <script type=&qu ...
- jQuery中的prop和attr区别
最近在做一个项目用jq时发现一个问题 在谷歌中可以正常出效果 但是在火狐中就是不行 就是这个prop和attr 之前用的是attr方法 但是在火狐中不出效果 于是特意看了两者的区别 主要 ...