题目连接:https://www.luogu.org/problemnew/show/P3377

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 300001 + 10;
struct Left_Tree{
int val, fa, son[2], dis;
}h[maxn<<2];
int n, m;
int Merge(int r1, int r2)
{
if(r1 == 0 || r2 == 0) return r1 + r2;
if(h[r1].val > h[r2].val) swap(r1, r2);
h[r1].son[1] = Merge(h[r1].son[1], r2);
h[h[r1].son[1]].fa = r1;
if(h[h[r1].son[0]].dis < h[h[r1].son[1]].dis) swap(h[r1].son[0], h[r1].son[1]);
h[r1].dis = h[h[r1].son[1]].dis + 1;
return r1;
}
void destory(int r1)
{
h[r1].val = -1;
h[h[r1].son[0]].fa = h[h[r1].son[1]].fa = 0;
Merge(h[r1].son[0], h[r1].son[1]);
}
int find(int x)
{
while(h[x].fa)
x = h[x].fa;
return x;
}
int main()
{
scanf("%d%d",&n,&m);
//h[0].dis = -1;
for(int i = 1; i <= n; i++)
scanf("%d",&h[i].val);
for(int i = 1; i <= m; i++)
{
int opt, x, y;
scanf("%d",&opt);
if(opt == 1)
{
scanf("%d%d",&x,&y);
if(h[x].val == -1 || h[y].val == -1) continue;
int t1 = find(x), t2 = find(y);
if(t1 != t2)
Merge(t1, t2);
}
else
{
scanf("%d",&x);
if(h[x].val == -1) printf("-1\n");
else
{
int t1 = find(x);
printf("%d\n",h[t1].val);
destory(t1);
}
}
}
return 0;
}

【luogu P3377 左偏树(可并堆)】 模板的更多相关文章

  1. 洛谷 [P3377] 左偏树(可并堆)

    可并堆,就是可以合并的堆 注意并查集不能路径压缩,不然删除根节点时会出错 #include <iostream> #include <cstring> #include < ...

  2. [note]左偏树(可并堆)

    左偏树(可并堆)https://www.luogu.org/problemnew/show/P3377 题目描述 一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 ...

  3. 2021.08.01 P3377 左偏树模板

    2021.08.01 P3377 左偏树模板 P3377 [模板]左偏树(可并堆) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) #include<iostream> ...

  4. bzoj2809 [Apio2012]dispatching——左偏树(可并堆)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2809 思路有点暴力和贪心,就是 dfs 枚举每个点作为管理者: 当然它的子树中派遣出去的忍者 ...

  5. HDU3031 To Be Or Not To Be 左偏树 可并堆

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3031 题意概括 喜羊羊和灰太狼要比赛. 有R次比赛. 对于每次比赛,首先输入n,m,n表示喜羊羊和灰 ...

  6. HDU5818 Joint Stacks 左偏树,可并堆

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU5818 题意概括 有两个栈,有3种操作. 第一种是往其中一个栈加入一个数: 第二种是取出其中一个栈的顶 ...

  7. BZOJ 4003: [JLOI2015]城池攻占 左偏树 可并堆

    https://www.lydsy.com/JudgeOnline/problem.php?id=4003 感觉就是……普通的堆啊(暴论),因为这个堆是通过递归往右堆里加一个新堆或者新节点的,所以要始 ...

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

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

  9. BZOJ 5494: [2019省队联测]春节十二响 (左偏树 可并堆)

    题意 略 分析 稍微yy一下可以感觉就是一个不同子树合并堆,然后考场上写了一发左偏树,以为100分美滋滋.然而发现自己傻逼了,两个堆一一对应合并后剩下的一坨直接一次合并进去就行了.然鹅我这个sb把所有 ...

随机推荐

  1. 01-struts2配置详解

    1 struts.xml配置详解 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE strut ...

  2. IE678不兼容CSS3 user-select:none(不可复制功能),需要JS解决

    [方法一:CSS3实现文本不可复制] .content {-moz-user-select:none;-webkit-user-select:none;-ms-user-select:none;-o- ...

  3. HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】

    Can you answer these queries? Time Limit:2000MS     Memory Limit:65768KB     64bit IO Format:%I64d & ...

  4. PHP通过映射类查找类所在文件

    $reflectObj = new ReflectionClass(类名); $file_name = $reflectObj->getFileName(); var_dump($file_na ...

  5. c#-IO和序列化操作

    IO 用到的命名空间:using System.IO; 文件和目录的管理! File类 FileInfo类 Directory类 DirectoryInfo类 操作文件的类! FileStream{ ...

  6. C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)

    最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...

  7. PAT 1043 Is It a Binary Search Tree

    #include <cstdio> #include <climits> #include <cstdlib> #include <vector> co ...

  8. PAT 1076 Forwards on Weibo

    #include <cstdio> #include <cstdlib> #include <vector> #include <queue> #inc ...

  9. js带文字的圆随机运动

    首先是html代码(其实就只有一个画布,记得要把外部js引入写在body底部 <!doctype html> <html> <head> <meta http ...

  10. .NET开源工作流RoadFlow-表单设计-数据表格

    数据表格即在表单中显示一个table,该table数据可以来自任意自定义的来源: 数据类型:指定表格的数据源类型 1.datatable,即.net中的System.Data.DataTable 2. ...