LG3377 【模板】左偏树(可并堆)
好博客
上面那个用数组写的跑的快,且便于封装,就用他的代码了。
代码
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x){
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff;
struct LeftTree
{
static const int MAXN=1e5+7;
int ch[MAXN][2],fa[MAXN];
int val[MAXN],dis[MAXN];
int merge(int x,int y)
{
if(x==0||y==0)
return x+y;
if(val[x]>val[y]||(val[x]==val[y]&&x>y))
swap(x,y);
ch[x][1]=merge(ch[x][1],y);
fa[ch[x][1]]=x;
if(dis[ch[x][0]]<dis[ch[x][1]])
swap(ch[x][0],ch[x][1]);
dis[x]=dis[ch[x][1]]+1;
return x;
}
int find(int x)
{
while(fa[x])
x=fa[x];
return x;
}
void pop(int x)
{
val[x]=-1;
fa[ch[x][0]]=fa[ch[x][1]]=0;
merge(ch[x][0],ch[x][1]);
}
}LT;
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int n,m;
read(n);read(m);
for(int i=1;i<=n;++i)
read(LT.val[i]);
while(m--)
{
int opt;
read(opt);
if(opt==1)
{
int x,y;
read(x);read(y);
if(LT.val[x]==-1||LT.val[y]==-1)
continue;
int fx=LT.find(x),fy=LT.find(y);
if(fx==fy)
continue;
LT.merge(fx,fy);
}
else if(opt==2)
{
int x;
read(x);
if(LT.val[x]==-1)
puts("-1");
else
{
int fx=LT.find(x);
printf("%d\n",LT.val[fx]);
LT.pop(fx);
}
}
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
LG3377 【模板】左偏树(可并堆)的更多相关文章
- [note]左偏树(可并堆)
左偏树(可并堆)https://www.luogu.org/problemnew/show/P3377 题目描述 一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 ...
- bzoj2809 [Apio2012]dispatching——左偏树(可并堆)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2809 思路有点暴力和贪心,就是 dfs 枚举每个点作为管理者: 当然它的子树中派遣出去的忍者 ...
- [luogu3377][左偏树(可并堆)]
题目链接 思路 左偏树的模板题,参考左偏树学习笔记 对于这道题我是用一个并查集维护出了哪些点是在同一棵树上,也可以直接log的往上跳寻找根节点 代码 #include<cstdio> #i ...
- HDU3031 To Be Or Not To Be 左偏树 可并堆
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3031 题意概括 喜羊羊和灰太狼要比赛. 有R次比赛. 对于每次比赛,首先输入n,m,n表示喜羊羊和灰 ...
- HDU5818 Joint Stacks 左偏树,可并堆
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU5818 题意概括 有两个栈,有3种操作. 第一种是往其中一个栈加入一个数: 第二种是取出其中一个栈的顶 ...
- BZOJ 4003: [JLOI2015]城池攻占 左偏树 可并堆
https://www.lydsy.com/JudgeOnline/problem.php?id=4003 感觉就是……普通的堆啊(暴论),因为这个堆是通过递归往右堆里加一个新堆或者新节点的,所以要始 ...
- Monkey King(左偏树 可并堆)
我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...
- 洛谷 P3377 模板左偏树
题目:https://www.luogu.org/problemnew/show/P3377 左偏树的模板题: 加深了我对空 merge 的理解: 结构体的编号就是原序列的位置. 代码如下: #inc ...
- BZOJ 5494: [2019省队联测]春节十二响 (左偏树 可并堆)
题意 略 分析 稍微yy一下可以感觉就是一个不同子树合并堆,然后考场上写了一发左偏树,以为100分美滋滋.然而发现自己傻逼了,两个堆一一对应合并后剩下的一坨直接一次合并进去就行了.然鹅我这个sb把所有 ...
- BZOJ1455 罗马游戏 左偏树 可并堆
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1455 题意概括 n个人,2种操作. 一种是合并两个人团,一种是杀死某一个人团的最弱的人. 题解 左 ...
随机推荐
- 只输FLOAT值 TEXTBOX
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (in ...
- English trip V1 - 8.What's in My Bag? 我的包里面有什么? Teacher:Corrine Key: plular(复数) and singular(单数)
In this lesson you will learn to talk about the things you have. 您将学习如何谈论您拥有的东西 课上内容(Lesson) What' ...
- English trip -- VC(情景课)9 C What are they doing? 他们在做什么?
Grammar focus 语法点: 以What 开头的问句 What is/is/are he/she/they doing? Cutting the grass. Walking the dog. ...
- Vue.js Cookbook: 添加实例属性; 👍 axios(4万➕✨)访问API; filters过滤器;
add instance properties //加上$,防止和已经定义的data,method, computed的名字重复,导致被覆写.//可以自定义添加其他符号. Vue.prototype. ...
- C++中基类虚析构函数的作用及其原理分析
虚析构函数的理论前提是 执行完子类的析构函数,那么父类的虚构函数必然会被执行. 那么当用delete释放一个父类指针所实例化的子类对象时,如果没有定义虚析构函数,那么将只会调用父类的析构函数,而不会调 ...
- eclipse properties 文件查看和编辑插件 Properties Editor
Properties Edito官网地址:http://propedit.sourceforge.jp/index_en.html Properties Edito安装地址:http://proped ...
- CSS3提供的transition动画
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- PADS Layout如何进行“ECO对比更新”
我们在画PCB中,经常会遇到要修改封装等操作.不推荐直接在pcb中非ECO模式下修改,这样会和orcad原理图不同步.我们采用修改orcad原理图,然后由pads layout软件来自动修改pads ...
- Myeclipse快键键
------------------------------------MyEclipse 快捷键1(CTRL)-------------------------------------Ctrl+1 ...
- struts1的parameter
1.配置文件 parameter="method" 2.请求路径 http://localhost:8081/purchaseDeclareAction.do?me ...