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