zoukankan      html  css  js  c++  java
  • 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
    "); 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
    ", t[rt].val);
    		}
    		
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    年薪百万必备能力
    二叉搜索树
    字符串和字符串模式匹配
    2006最后寄语
    “豆瓣”式推荐
    什么是LOMO?
    大国崛起
    马季之死
    时间的价值(The Value Of Time)
    我读雅虎的“花生酱宣言”
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11186011.html
Copyright © 2011-2022 走看看