LuoguP1456 Monkey King (左偏树)
struct LeftTree{
int l,r,val,dis;
}t[N];
int fa[N];
inline int Find(int x){
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
inline int Merge(int x, int y){
if(!x) return y;
if(!y) return x;
if(t[x].val < t[y].val || (t[x].val == t[y].val && x > y)) Swap(x, y);
t[x].r = Merge(t[x].r, y);
fa[t[x].r] = fa[t[x].l] = x;
if(t[t[x].r].dis > t[t[x].l].dis) Swap(t[x].r, t[x].l);
t[x].dis = t[t[x].r].dis + 1;
return x;
}
int main(){
int n,m;
while(~scanf("%d", &n)){
R(i,1,n){
t[i].l = t[i].r = t[i].dis = 0;
io >> t[i].val;
fa[i] = i;
}
io >> m;
while(m--){
int x,y;
io >> x >> y;
x = Find(x), y = Find(y);
if(x == y){
printf("-1\n"); continue;
}
t[x].val >>= 1;
int rt = Merge(t[x].l, t[x].r); // the left and right united and elected the new king
t[x].l = t[x].r = 0; // the old king lost everything
x = Merge(x, rt); // the fight between the old and the new, only the winner was deserved to be king
t[y].val >>= 1;
rt = Merge(t[y].l, t[y].r);
t[y].l = t[y].r = 0;
y = Merge(y, rt);
rt = Merge(x, y); // the winner of the two groups
fa[x] = fa[y] = rt; // ought to rule the union
printf("%d\n", t[rt].val);
}
}
return 0;
}
LuoguP1456 Monkey King (左偏树)的更多相关文章
- hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...
- ZOJ2334 Monkey King 左偏树
ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...
- zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...
- HDU1512 ZOJ2334 Monkey King 左偏树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...
- HDU 1512 Monkey King (左偏树+并查集)
题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...
- hdu 1512 Monkey King —— 左偏树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
- HDU 1512 Monkey King ——左偏树
[题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...
- HDU 1512 Monkey King(左偏堆)
爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...
随机推荐
- singlelinklist
C++实现单链表 阅读先知 链表是一种动态数据结构,他的特点是用一组任意的存储单元(可以是连续的,也可以是不连续的)存放数据元素. 链表中每一个元素成为"结点",每一个结点都是由数 ...
- Vue最新防抖方案
函数防抖(debounce):当持续触发事件时,一定时间段内没有再触发事件,事件处理函数才会执行一次,如果设定的时间到来之前,又一次触发了事件,就重新开始延时.举个栗子,持续触发scroll事件时,并 ...
- MySQL-4-DDL
DDL:数据定义语言 创建create 创建库 语法:create database [if not exists]库名 # 创建库 CREATE DATABASE IF NOT EXISTS boo ...
- SAP SD-Invoice 销售发票
针对销售订单的发票流程: 1. 事务码:VF01(个别生成系统发票) 创建开票凭证(发票)/ VF04 开具系统发票(可把多个item 合并成一张系统发票) 2. 事务码:VF02 修改发票, 释放 ...
- Linux 文件的打包压缩
压缩和解压 压缩:为了节约磁盘空间. gzip --- .gz bzip2 --- .bz2 xz --- .xz compress --- .z 压缩比例:xz > bzip2 > gz ...
- rhel安装vmtools
第一步,vmware登录虚拟机,菜单栏找到"虚拟机"--"安装TOOLS" //如果打开虚拟机的光驱后没有文件.那么重复以上操作. 第二步,拷贝压缩文件到桌面: ...
- python小题目练习(七)
题目:实现如下图所示结果 代码实现: """Author:mllContent:模拟火车订票系统Date:2020-11-16"""# 定义 ...
- Linux操作系统(5):网络命令
Linux 网络环境配置①自动获取 缺点: linux 启动后会自动获取 IP,缺点是每次自动获取的 ip 地址可能不一样.这个不适用于做服务器,因为我们的服务器的 ip 需要时固定的. ②直 接 修 ...
- 【小程序自动化Minium】三、元素定位- WXSS 选择器的使用
最近更新略疲,主要是业余时间多了几个变化.比如忙活自己的模拟赛车驾舱升级.还跟朋友筹备一个小程序项目.另外早上的时间留给背单词了... 上一章中讲到Page接口的get_element()与get_e ...
- DENIED Redis is running in protected mode because protected mode is enabled
DENIED Redis is running in protected mode because protected mode is enabled redisson连接错误 Unable to i ...