您的位置:首页 > 其它

[BZOJ1455]罗马游戏(可并堆)

2017-01-06 19:49 429 查看

题目描述

传送门

题解

左偏树裸题

splay启发式合并是不是太慢了?

注意!

如果有一个人x被杀掉了,那这个点就没有用了,这个点不能留在这一棵左偏树里

但是必须将x的代表元素改成新的树根,因为在原树里很多点的代表元素是指向x的,如果不修改的话就相当于把这些点指向了一个空节点

合并两棵之前没有交集的子树的时候就可以直接将把两个根的代表元素指向新的根,这样就保证了所有的点都指向了新根

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 1000005

int n,m,x,y;
char opt;
int key
,f
,dis
,ls
,rs
;
bool flag
;

int find(int x)
{
if (x==f[x]) return x;
f[x]=find(f[x]);
return f[x];
}
int merge(int x,int y)
{
if (!x) return y;
if (!y) return x;
if (key[x]>key[y]) swap(x,y);
rs[x]=merge(rs[x],y);
if (dis[ls[x]]<dis[rs[x]]) swap(ls[x],rs[x]);
if (!rs[x]) dis[x]=0;
else dis[x]=dis[rs[x]]+1;
return x;
}
void pop(int x)
{
f[x]=merge(ls[x],rs[x]);
f[f[x]]=f[x];
ls[x]=rs[x]=dis[x]=key[x]=0;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;++i) scanf("%d",&key[i]);
for (int i=1;i<=n;++i) f[i]=i;
scanf("%d",&m);
for (int i=1;i<=m;++i)
{
char opt=getchar();
while (opt!='M'&&opt!='K') opt=getchar();
if (opt=='M')
{
scanf("%d%d",&x,&y);
if (flag[x]||flag[y]) continue;
int fx=find(x),fy=find(y);
if (fx!=fy)
{
int top=merge(fx,fy);
f[fx]=f[fy]=top;
}
}
else
{
scanf("%d",&x);
if (flag[x]) {puts("0");continue;}
int fx=find(x);
flag[fx]=true;
printf("%d\n",key[fx]);
pop(fx);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: