Problem Description
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can't avoid quarrelling, and it only happens between two monkeys who does not
know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these
monkeys even if they have ever conflicted.



Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).



And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.
 
Input
There are several test cases, and each case consists of two parts.



First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).



Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.


 
Output
For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.
 
Sample Input
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
 
Sample Output
8
5
5
-1
10

正解:左偏树

左偏树板子题。。每次删除树根,键值除以2后再插入树,并且将两棵树合并。

//It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define inf 1<<30
#define il inline
#define RG register
#define ll long long
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout) using namespace std; struct left_tree{ int fa,ls,rs,dis,key; }ltree[100010]; int x,n,m; il int gi(){
RG int x=0,q=0; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
if (ch=='-') q=1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
} il int father(RG int x){ if (ltree[x].fa!=x) return father(ltree[x].fa); else return x; } il void build(RG int x,RG int k){ ltree[x]=(left_tree){x,0,0,0,k}; return; } il int merge(RG int x,RG int y){
if (!x) return y; if (!y) return x; if (ltree[x].key<ltree[y].key) swap(x,y);
ltree[x].rs=merge(ltree[x].rs,y); RG int &l=ltree[x].ls,&r=ltree[x].rs;
ltree[l].fa=ltree[r].fa=x; if (ltree[l].dis<ltree[r].dis) swap(l,r);
if (!r) ltree[x].dis=0; else ltree[x].dis=ltree[r].dis+1; return x;
} il int del(RG int rt){
RG int l=ltree[rt].ls,r=ltree[rt].rs; ltree[l].fa=l,ltree[r].fa=r;
ltree[rt].dis=ltree[rt].ls=ltree[rt].rs=0; return merge(l,r);
} il int query(RG int x,RG int y){
RG int l=del(x),r=del(y); ltree[x].key>>=1,ltree[y].key>>=1;
l=merge(l,x),r=merge(r,y),l=merge(l,r); return ltree[l].key;
} il void work(){
for (RG int i=1;i<=n;++i) x=gi(),build(i,x); m=gi();
for (RG int i=1;i<=m;++i){
RG int u=gi(),v=gi(),a=father(u),b=father(v);
if (a==b){ printf("-1\n"); continue; }
printf("%d\n",query(a,b));
}
return;
} int main(){
File("monkeyking");
while (scanf("%d",&n)!=EOF) work();
return 0;
}

hdu1512 Monkey King的更多相关文章

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

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

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

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

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

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

  4. ZOJ 2334 Monkey King

    并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子                       Monkey King ...

  5. 数据结构(左偏树):HDU 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. P1456 Monkey King

    题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...

  7. HDU - 5201 :The Monkey King (组合数 & 容斥)

    As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...

  8. Monkey King(左偏树 可并堆)

    我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...

  9. 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

随机推荐

  1. 一个服务器启动多个tomcat(详细图解)

    1.官网下载一个tomcat,复制一个副本(第二个tomcat) 2.添加2个环境变量(右键单击我的电脑->选择属性->选择高级->选择环境变量),是2个tomcat的位置,环境变量 ...

  2. linux apache添加多站点配置(Ubuntn和Centos)

     Linux Apache 多站点配置 Centos 配置方式: 找到 /etc/httpd/conf/http.conf 添加监听端口,eg: Listen 89 虚拟机配置,一个端口对应一个 &l ...

  3. [SQL] SQL 基础知识梳理(六)- 函数、谓词、CASE 表达式

    SQL 基础知识梳理(六)-  函数.谓词.CASE 表达式 目录 函数 谓词 CASE 表达式 一.函数 1.函数:输入某一值得到相应输出结果的功能,输入值称为“参数”,输出值称为“返回值”. 2. ...

  4. gradient渐变IE兼容处理

    根据caniuse(http://caniuse.com/#search=gradient),rgba兼容性为IE10以及以上浏览器. 实例代码: <!doctype html> < ...

  5. Repaints and Reflows 重绘和重排版

    当浏览器下载完所有页面HTML标记,JavaScript,CSS,图片之后,它解析文件并创建两个内部数据 一棵DOM树 表示页面结构 Normal 0 7.8 磅 0 2 false false fa ...

  6. 【iOS】7.4 定位服务->2.1.4 定位 - 官方框架CoreLocation 案例:指南针效果

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  7. MySQL从库忽略某些错误

    z熬配置MySQL主从同步的时候常常会因为主库的中SQL语句的错误造成从库的同步出现错误,一旦从库同步出现错误就会造成同步的卡壳影响后续的同步: 可以在从库的配置文件中加入如下的参数,使从库可以自动忽 ...

  8. Python 一些有趣的技巧哦!

    #Python 技巧命令 python 如一股清流,可以说屌到飞起,下面咱就来看看一些屌的东西 ### python2 最简单的web服务 ` python -m SimpleHTTPServer 8 ...

  9. unity插件开发——一个例子:简单的svn集成

    在unity开发过程中,通常我们习惯性地在Windows操作系统下使用svn进行版本管理,而每次提交更新,都需要回到文件夹下的这种操作让人无法忍受.是不是可以集成svn到unity中呢?查了一圈uni ...

  10. 在亚马逊Red Hat 7.1 linux上安装mysql

      安装前检查之前是否安装并卸载之前的和删除关联文件 rpm -qa|grep mysql     yum remove mysql mysql-server mysql-libs mysql-com ...