题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=1455

浅谈左偏树:https://www.cnblogs.com/AKMer/p/10246635.html

左偏树模板题。

时间复杂度:\(O(nlogn)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std; const int maxn=1e6+5; int n,m;
int son[maxn][2];
int v[maxn],fa[maxn],dist[maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} int find(int x) {
while(fa[x])x=fa[x];
return x;
} int merge(int a,int b) {
if(!a||!b)return a+b;
if(v[a]>v[b])swap(a,b);
son[a][1]=merge(son[a][1],b);
fa[son[a][1]]=a;
if(dist[son[a][1]]>dist[son[a][0]])
swap(son[a][1],son[a][0]);
dist[a]=dist[son[a][1]]+1;
return a;
} void pop(int u) {
printf("%d\n",v[u]);v[u]=-1;
fa[son[u][0]]=fa[son[u][1]]=0;
merge(son[u][0],son[u][1]);
son[u][0]=son[u][1]=0;
} int main() {
n=read();
for(int i=1;i<=n;i++)
v[i]=read();
m=read();
for(int i=1;i<=m;i++) {
char s[10];
scanf("%s",s+1);
if(s[1]=='M') {
int x=read(),y=read();
if(v[x]==-1||v[y]==-1)continue;
x=find(x),y=find(y);
if(x==y)continue;
merge(x,y);
}
else {
int u=read();
if(v[u]==-1) {puts("0");continue;}
u=find(u),pop(u);
}
}
return 0;
}

最新文章

  1. C#开发微信门户及应用(9)-微信门户菜单管理及提交到微信服务器
  2. iOS判断程序在前台还是后台
  3. 黑马程序员-c语言-类型强制转换
  4. jQuery不支持hashchange事件?
  5. 获取bundle目录下的所有图片文件名
  6. poj 2528Mayor&#39;s posters
  7. Qt plugin系统的几点说明
  8. 用户交互式命令:read
  9. noi2015 day1 T2软件包管理器
  10. http://codeforces.com/contest/834
  11. C#中级-通过注册表读取Windows Service程序执行路径
  12. Web 性能优化: 使用 Webpack 分离数据的正确方法
  13. iOS- UITextView与键盘回收与键盘遮挡输入框
  14. HDU 6015 Skip the Class 优先队列 map的使用
  15. java发送http连接
  16. CTreeCtrl获得鼠标点击时的节点
  17. 《mahout实战》
  18. OpenCV学习笔记——图像平滑处理
  19. Linux mkdir 如何递归创建目录?
  20. ArrayList动态扩容机制

热门文章

  1. ajax (异步js+xml)
  2. Linux根目录下重要文件夹
  3. linux c编程:信号(四) sigaction
  4. android 中使用svg
  5. SSAS(SQL Server 分析服务)、***S(SQL Server报表服务)、SSIS(SQL Server集成服务)
  6. 海信电视 LED55K370 升级固件总结【含固件下载地址】
  7. PHP数组各种操作与函数汇总
  8. 修改SecureCRT终端的Home和End功能键
  9. Codeforces 158E Phone Talks:dp
  10. Java的TCP网络编程