题目大意:有n个猴子。每个猴子有一个力量值,力量值越大表示这个猴子打架越厉害。如果2个猴子不认识,他们就会找他们认识的猴子中力量最大的出来单挑,单挑不论输赢,单挑的2个猴子力量值减半,这2拨猴子就都认识了,不打不相识嘛。现在给m组询问,如果2只猴子相互认识,输出-1,否则他们各自找自己认识的最牛叉的猴子单挑,求挑完后这拨猴子力量最大值。

左偏大根加并查

代码都懒的贴了...

 #include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <utility>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;
const int maxn = ;
struct node {
int l,r,dis,val,par;
} heap[maxn];
int N, M;
int find ( int &x ) {
return heap[x].par == x ? x : heap[x].par = find ( heap[x].par );
}
int merge(int rt1,int rt2)
{
if (rt1==) return rt2;
if (rt2==) return rt1;
if ( heap[rt2].val>heap[rt1].val )swap(rt1,rt2);
heap[rt1].r = merge(heap[rt1].r,rt2);
heap[heap[rt1].r].par = rt1;
if ( heap[ heap[rt1].l ].dis < heap[ heap[rt2].r ].dis )
swap ( heap[rt1].l, heap[rt1].r );
else heap[rt1].dis = heap[ heap[rt1].r ].dis + ;
return rt1;
}
int push ( int x, int y ) {
return merge ( x, y );
}
int pop ( int &x ) {
int l = heap[x].l;
int r = heap[x].r;
heap[l].par = l;
heap[r].par = r;
heap[x].l = heap[x].r = heap[x].dis = ;
return merge ( l, r );
}
bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
}
int main() {
while ( scan_d ( N ) ) {
for ( int i = ; i <= N; ++ i ) {
scan_d ( heap[i].val );
heap[i].l = heap[i].r = heap[i].dis = ;
heap[i].par = i;
}
scan_d ( M );
int a, b, x, y;
while ( M -- ) {
scan_d (a); scan_d (b);
x = find ( a );
y = find ( b );
if ( x == y ) {
puts ( "-1" );
} else {
heap[x].val /= ;
int px = push ( pop ( x ), x );
heap[y].val /= ;
int py = push ( pop ( y ), y ); printf ( "%d\n", heap[ merge ( px, py ) ].val );
}
}
}
return ;
}

HDOJ 1512 几乎模板的左偏树的更多相关文章

  1. 洛谷 P3377 【模板】左偏树(可并堆)

    洛谷 P3377 [模板]左偏树(可并堆) 题目描述 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或 ...

  2. 模板 可并堆【洛谷P3377】 【模板】左偏树(可并堆)

    P3377 [模板]左偏树(可并堆) 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或第y个数已经被删 ...

  3. 洛谷P3377 【模板】左偏树(可并堆) 题解

    作者:zifeiy 标签:左偏树 这篇随笔需要你在之前掌握 堆 和 二叉树 的相关知识点. 堆支持在 \(O(\log n)\) 的时间内进行插入元素.查询最值和删除最值的操作.在这里,如果最值是最小 ...

  4. HDU 1512 Monkey King(左偏树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: 有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了 ...

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

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...

  6. [Luogu3377]【模板】左偏树(可并堆)

    题面戳我 题目描述 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或第y个数已经被删除或第x和第y个数 ...

  7. luogu【P3377】 【模板】左偏树

    左偏树 顾名思义 向左偏的树 (原题入口) 它有啥子用呢??? 当然是进行堆的合并啦2333普通堆的合并其实是有点慢的(用优先队列的话 只能 一个pop 一个push 来操作 复杂度就是O(n log ...

  8. HDU 1512 Monkey King(左偏树)

    Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does thi ...

  9. [洛谷P3377]【模板】左偏树(可并堆)

    题目大意:有$n$个数,$m$个操作: $1\;x\;y:$把第$x$个数和第$y$个数所在的小根堆合并 $2\;x:$输出第$x$个数所在的堆的最小值 题解:左偏树,保证每个的左儿子的距离大于右儿子 ...

随机推荐

  1. CMD规范

    define(function (require, exports, module) { module.exports = require('xx/xx/xx')({}); });

  2. 11 TCP/IP 基础与Linux的网络配置

    1. TCP/IP与OSI参考模型 TCP/IP是Unix/Linux世界的网络基础,在某种意义上Unix网络就是TCP/IP,而TCP/IP就是网络互联的标准.它不是一个独立的协议,而是一组协议.其 ...

  3. Oracle数据库的后备和恢复————关于检查点的一些知识

    当我们使用一个数据库时,总希望数据库的内容是可靠的.正确的,但由于计算机系统的故障(硬件故障.软件故障.网络故障.进程故障和系统故障)影响数据库系统的操作,影响数据库中数据的正确性,甚至破坏数据库,使 ...

  4. ADF_Desktop Integration系列3_ADF桌面集成入门之重定义ADF Desktop Excel

    2013-05-01 Created By BaoXinjian

  5. linux装载可执行程序简析

    朱宇轲 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 linux中主要 ...

  6. 自动improt的xcode插件 Auto-Importer

    https://github.com/lucholaf/Auto-Importer-for-Xcode

  7. impala简单使用

    impala-shell connect ha1:21000 更新元信息 invalidate metadata;

  8. ReSharper 文件注释

    添加文件注释方法如下: 打开菜单RESHARPER->Options->Code Editing –> File Header Text 如图所示,在其中空白处添加对应文件头注释, ...

  9. 2016-07-15: Window定时器使用

    windows下定时器使用实例 #include <iostream> #include <Windows.h> using namespace std; void Timer ...

  10. win10内网外网智能访问

    当电脑同时连接有线和WiFi时(有线连接为内网,WiFi为外网),会出现内网和外网内容无法同时访问的情况. 本方法实现内网和外网的同时访问. 第一步: 输入指令 “route print ” 查看路由 ...