HDU 1512 Monkey King

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1512

水题,就是在考察左偏树,不多说,直接上代码就是了。

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1e5+5;

struct Node{
	int l,r,f,v,h;
}node[MAXN];
int merge(int a,int b){
	if(a==0) return b;
	if(b==0) return a;
	if(node[a].v<node[b].v) swap(a,b);
	node[a].r=merge(b,node[a].r);
	node[node[a].r].f=a;
	if(node[node[a].l].h<node[node[a].r].h) swap(node[a].l,node[a].r);
	if(node[a].r==0) node[a].h=0;
	else node[a].h=node[node[a].r].h+1;
	return a;
}
int pop(int u){
	int l=node[u].l;
	int r=node[u].r;
	node[l].f=l;
	node[r].f=r;
	node[u].h=node[u].l=node[u].r=0;
	return merge(l,r);
}
int find(int u){ return node[u].f==u?u:find(node[u].f); }

int main(){
	int n;
	while(~scanf("%d",&n)){
		int stg;
		for(int i=1;i<=n;++i){
			scanf("%d",&stg);
			node[i].v=stg;
			node[i].f=i;
			node[i].l=node[i].r=node[i].h=0;
		}
		int q;
		scanf("%d",&q);
		while(q--){
			int a,b;
			scanf("%d%d",&a,&b);
			a=find(a);
			b=find(b);
			if(a==b){
				puts("-1");continue;
			}
			node[a].v/=2;
			node[b].v/=2;
			int a1=pop(a);
			int b1=pop(b);
			a=merge(a,a1);
			b=merge(b,b1);
			printf("%d\n",node[merge(a,b)].v);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值