bzoj1455左偏树裸题

#include <stdio.h>
bool vi[1000010];
int n,de[1000010],ls[1000010],rs[1000010],va[1000010],fa[1000010];
inline int read(){
	int x=0,c=getchar();
	for(;c<48||c>57;c=getchar());
	for(;c>47&&c<58;x=(x<<1)+(x<<3)+c-48,c=getchar());
	return x;
}
inline void swap(int &x,int &y){x^=y;y^=x;x^=y;}
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int merge(int x,int y){
	if(!x)return y;if(!y)return x;
	if(va[x]>va[y])swap(x,y);
	rs[x]=merge(rs[x],y);
	if(de[rs[x]]>de[ls[x]])swap(ls[x],rs[x]);
	de[x]=de[rs[x]]+1;
	return x;
}
int main(){
	n=read();
	for(int i=1;i<=n;fa[i]=i,va[i]=read(),i++);
	for(int x,y,t,m=read();m;m--){
		char s[4];
		scanf("%s",s);
		if(s[0]=='M'){
			x=read(),y=read();
			if(vi[x]||vi[y])continue;
			x=find(x),y=find(y);
			if(x!=y)t=merge(x,y),fa[x]=fa[y]=t;
		}
		else{
			x=read();
			if(vi[x]){putchar('0'),putchar('\n');continue;}
			t=find(x),vi[t]=1,printf("%d\n",va[t]),fa[t]=merge(ls[t],rs[t]),fa[fa[t]]=fa[t];
		}
	}
}

  

posted @ 2017-02-22 17:11  keshuqi  阅读(253)  评论(0编辑  收藏  举报