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只好斗的猴子.开始,他们各 ...
随机推荐
- 「POI2012」井 Well
description 你要挖井,\(n\)个地面的高度可视为\(h_i\),每次操作你可以将一个\(h_i-1\),你最多可执行\(m\)次操作. 你要任选其中一个\(h_i\)挖到\(0\),问你 ...
- ES6 - promise(2)
从上一篇中我们知道promise的概念,上一篇也提到了 promise的过程: 启动异步任务 => 返回promise对象 =>给promise对象绑定回调函数(甚至可以在异步任务结束后指 ...
- 【题解】Codeforces Round #798 (Div. 2)
本篇为 Codeforces Round #798 (Div. 2) 也就是 CF1689 的题解,因本人水平比较菜,所以只有前四题 A.Lex String 题目描述 原题面 给定两个字符串 \(a ...
- spring boot用ide新建项目遇到的restcontroller不能导入的问题
才开始学习spring boot,第一个程序helloworld就碰到@RestController和@RequestMapping(/hello)的注解都会报错的问题. 我个人的解决方法: 1.sp ...
- 26.MySQL数据库基础
MySQL数据库基础 目录 MySQL数据库基础 数据库的概念 数据 表 数据库 数据库的管理系(DBMS) 数据库系统 访问数据库的流程 数据库系统发展史 当今主流数据库介绍 关系数据库 关系数据库 ...
- 打通web的三维国产引擎!老子云AMRT,够牛!
AMRT(Auto Mobile Reality Technology)指的是自动化移动现实技术,它是老子云3D模型自动轻量化引擎及轻量化模型格式.模型展示框架.API/SDK的统称.3D研发技术其中 ...
- 智慧机房3D可视化技术解决方案
随着夏季气温越来越高,机房内大量设备同步工作时,难免使机房内温度飙升. 机房温度每升高10℃,计算机的可靠性就下降25% 磁盘磁带也会因热涨效应造成记录错误 计算机的时钟主频在温度过高都会降低 UPS ...
- Error: $injector:modulerr Module Error
Failed to instantiate module app due to://实例化失败 Error: [$injector:modulerr] http://errors.angularjs. ...
- Ubuntu系统iptables安全防护整改计划
端口开放 默认防火墙是开放所有端口的,如果拿来做应用服务器,就很危险,所以要把防火墙用起来,只将需要的端口开放,ubuntu用的是iptables防火墙. iptables处理流程 iptables ...
- jieba分词原理解析:用户词典如何优先于系统词典
目标 查看jieba分词组件源码,分析源码各个模块的功能,找到分词模块,实现能自定义分词字典,且优先级大于系统自带的字典等级,以医疗词语邻域词语为例. jieba分词地址:github地址:https ...