题目连接: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. fiter 编码

    package com.itheima.web.filter; import java.io.IOException; import javax.servlet.Filter; import java ...

  2. java语言编程使用正则表达式来实现提取(美团 3-5年经验 15-30k 北京 hadoop高级工程)中的3-5和15-30

    不多说,直接上干货! 如有这样的一条数据进来:   美团 3-5年经验 15-30k 北京 hadoop高级工程 //正则表达式提取工资值,因为15-30k后面有k,3-5年经验,不干净 public ...

  3. ife task0003学习笔记(五):JavaScript面向对象

    JavaScript 支持函数式编程.闭包.基于原型的继承等高级功能.在 Java 等面向对象的语言中,this 关键字的含义是明确且具体的,即指代当前对象.而在 JavaScript 中,this ...

  4. LeetCode 319 ——Bulb Switcher——————【数学技巧】

    319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...

  5. Oracle 过程中变量赋值

    create or replace function get_sal1(id employees.employee_id%type) return number is sal employees.sa ...

  6. 从Linux服务器下载文件到本地

    通过安装xshell,连接服务器,通过以下命令可以方便的将服务器的文件下载到本地 #下载一个文件 sz filename #下载多个文件 sz filename1 filename2 #下载dir目录 ...

  7. ASP.NET内置对象-网页转向的4中方法

    ASP.NET页面跳转的方法 1.超链接 2.Response.Redirect(“Urlstring”) 3.Server.Transfer(“UrlString”) 4.PostBackUrl 1 ...

  8. 【学习笔记】开源日志记录工具log4j使用方法

    http://blog.csdn.net/zouqingfang/article/details/37558469 一.在MyEclipse中使用log4j的步骤比较简单,主要分为以下四个步骤: 1. ...

  9. Spring Data JPA简单使用

    用Spring Data JPA操作数据库 这份教程教你用Spring Data JPA从关系数据库mysql中存储和提取数据.总结来自https://spring.io/guides/gs/acce ...

  10. 运行jsp时,报错404

    The origin server did not find a current reprsentation for the target resource or is not willing to ...