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 (左偏树)的更多相关文章

  1. hdu 1512 Monkey King 左偏树

    题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...

  2. ZOJ2334 Monkey King 左偏树

    ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...

  3. zoj 2334 Monkey King/左偏树+并查集

    原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...

  4. HDU1512 ZOJ2334 Monkey King 左偏树

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

  5. HDU 1512 Monkey King (左偏树+并查集)

    题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...

  6. hdu 1512 Monkey King —— 左偏树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...

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

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

  8. HDU 1512 Monkey King ——左偏树

    [题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...

  9. HDU 1512 Monkey King(左偏堆)

    爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...

随机推荐

  1. IOI2020 国家集训队作业 泛做

    题号 题目名称 rating 算法 完成情况 CF504E Misha and LCP on Tree CF505E Mr.Kitayuta vs. Bamboos CF506E Mr.Kitayut ...

  2. vue组件传参的方法--bus事件总线

    定义:事件总线是实现vue任意组件之前传递参数的一种编程技巧,本质上就是组件的自定义事件.事件总线有很多种写法,具体的思路就是创造一个大家都可以访问到的公共的属性,在这个公共的属性上面可以调用$on, ...

  3. swap函数模板

    在许多应用程序中,都有交换相同类型的两个变量内容的需要.例如,在对整数数组进行排序时,将需要一个函数来交换两个变量的值,如下所示: void swap(int &a, int &b) ...

  4. Java-SpringBoot-使用多态给项目解耦

    Java-SpringBoot-使用多态给项目解耦 提及 今天在打算维护一下智慧社区这个项目的时候,想到项目是使用Satoken这个开箱即用的授权和认证的组件,因为在项目开启的时候对SpringSec ...

  5. Mybatisi和Spring整合源码分析

    一.MybatisSpring的使用 1.创建 Maven 工程. 2.添加依赖,代码如下 <dependency> <groupId>org.mybatis</grou ...

  6. SAP APO-PP / DS

    在SAP APO中,使用生产计划/详细计划(Production Planning/Detailed Scheduling)生成满足生产要求的采购建议. 此组件还用于定义资源计划和订单明细. 您还可以 ...

  7. VisionPro · C# · 加载与保存视觉工具包

    当项目程序被启动或更换程序产品型号时,我们需要提前加载对应的VisionPro的程序VPP文件,以提高程序调用效率. 加载代码: 1 using System; 2 using System.Wind ...

  8. 《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下

    1.前言 今天的理性认识主要就是讲解和分享Fiddler的一些理论基础知识.其实这部分也没有什么,主要是给小伙伴或者童鞋们讲一些实际工作中的场景,然后隆重推出我们的猪脚(主角)-Fiddler. 1. ...

  9. 常用的Linux命令和Git的必要配置

    常用的Linux命令平时一定要多使用这些基础的命令! 1.cd : 改变目录. 2.cd . . 回退到上一个目录,直接cd进入默认目录 3.pwd : 显示当前所在的目录路径. 4.ls(ll): ...

  10. springboot中实现权限认证的两个框架

    web开发安全框架 提供认证和授权功能! 一.SpringSecurity 1.导入依赖 <dependency> <groupId>org.springframework.b ...