【BZOJ】1455 罗马游戏 左偏树

110 篇文章 0 订阅
3 篇文章 0 订阅

题目传送门

这题和洛谷上的左偏树模板的解题思路是一模一样的,所以只要贴上左偏树的模板就好了。

附上AC代码:

#include <cstdio>
#include <cctype>
#include <algorithm>
#define N 1000010
using namespace std;

int n,m,dis[N],w[N],x,y,f[N],ls[N],rs[N];

void read(int& a){
	static char c=getchar();a=0;int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1;c=getchar();}
	while (isdigit(c)) a=a*10+c-'0',c=getchar();
}

int gf(int x){
	while (f[x]) x=f[x];
	return x;
}

int hb(int a,int b){
	if (a*b==0) return a+b;
	if (w[a]>w[b]||(w[a]==w[b]&&a>b)) swap(a,b);
	rs[a]=hb(rs[a],b);
	f[rs[a]]=a;
	if (dis[ls[a]]<dis[rs[a]]) swap(ls[a],rs[a]);
	dis[a]=dis[rs[a]]+1;
	return a;
}

int main(void){
	read(n),dis[0]=-1;
	for (int i=1; i<=n; ++i) read(w[i]);
	read(m);
	while (m--){
		char c=getchar();
		while (c!='M'&&c!='K') c=getchar();
		switch (c){
			case 'M':
				read(x),read(y);
				if (w[x]==-1||w[y]==-1) continue;
				x=gf(x),y=gf(y);
				if (x!=y) hb(x,y);
				break;
			case 'K':
				read(x);
				if (w[x]==-1) puts("0");
				else {
					x=gf(x),printf("%d\n",w[x]),w[x]=-1;
					f[ls[x]]=f[rs[x]]=0,hb(ls[x],rs[x]);
				}
				break;
		}
	}
}
orzLYF大佬,如此的强,在半个月前就把这题A掉了。别问我是怎么知道的。

(因为这题需要权限,而我又没有氪金,所以交这题时我用的是他的号。别问我是怎么知道他的密码的)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值