LuoguP1456 Monkey King (左偏树)

struct LeftTree{
	int l,r,val,dis;
}t[N];
int fa[N];
inline int Find(int x){
	return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
inline int Merge(int x, int y){
	if(!x) return y;
	if(!y) return x;
	if(t[x].val < t[y].val || (t[x].val == t[y].val && x > y)) Swap(x, y);
	t[x].r = Merge(t[x].r, y);
	fa[t[x].r] = fa[t[x].l] = x;
	if(t[t[x].r].dis > t[t[x].l].dis) Swap(t[x].r, t[x].l);
	t[x].dis = t[t[x].r].dis + 1;
	return x;
}

int main(){
	int n,m;
	while(~scanf("%d", &n)){
		
		R(i,1,n){
			t[i].l = t[i].r = t[i].dis = 0;
			io >> t[i].val;
			fa[i] = i;
		}
		
		io >> m;
		
		while(m--){
			int x,y;
			io >> x >> y;
			x = Find(x), y = Find(y);
			if(x == y){
				printf("-1\n"); continue;
			}
			
			t[x].val >>= 1;
			int rt = Merge(t[x].l, t[x].r); // the left and right united and elected the new king
			t[x].l = t[x].r = 0; // the old king lost everything
			x = Merge(x, rt); // the fight between the old and the new, only the winner was deserved to be king
			
			t[y].val >>= 1;
			rt = Merge(t[y].l, t[y].r);
			t[y].l = t[y].r = 0;
			y = Merge(y, rt);
			
			rt = Merge(x, y); // the winner of the two groups
			fa[x] = fa[y] = rt; // ought to  rule the union
			
			printf("%d\n", t[rt].val);
		}
		
	}
	
	return 0;
}
posted @ 2019-07-14 21:53  邱涵的秘密基地  阅读(134)  评论(0编辑  收藏  举报