zoukankan      html  css  js  c++  java
  • BZOJ 2733 HNOI 2012 永无乡 平衡树启示式合并

    题目大意:有一些岛屿,一開始由一些无向边连接。

    后来也有不断的无向边增加,每个岛屿有个一独一无二的重要度,问随意时刻的与一个岛屿联通的全部岛中重要度第k大的岛的编号是什么。


    思路:首先连通性一定要用并查集维护。然后就是联通快内的第k大问题,显然是平衡树。可是并查集的合并怎么搞?能够考虑按秩合并,这种话就保证每次在平衡树中处理的元素尽量的少,就能够水过这个题了。

    注意一下输出-1的推断。


    CODE:

    #include <map>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define MAX 100010
    #define SIZE(a) (a == NULL ? 0:a->size)
    using namespace std;
    
    map<int,int> G;
    
    struct Complex{
    	int val,random,size,cnt;
    	Complex *son[2];
    	
    	Complex(int _) {
    		val = _;
    		random = rand();
    		size = cnt = 1;
    		son[0] = son[1] = NULL;
    	}
    	int Compare(int x) {
    		if(x == val)	return -1;
    		return x > val;
    	}
    	void Maintain() {
    		size = cnt;
    		if(son[0] != NULL)	size += son[0]->size;
    		if(son[1] != NULL)	size += son[1]->size;
    	}
    };
    
    int points,edges,asks;
    int src[MAX];
    int father[MAX],cnt[MAX];
    
    Complex *tree[MAX];
    
    char c[10];
    
    int Find(int x);
    
    inline void Rotate(Complex *&a,bool dir);
    void Insert(Complex *&a,int x);
    void Delete(Complex *&a,int x);
    int Kth(Complex *a,int k);
    
    int main()
    {
    	cin >> points >> edges;
    	for(int i = 1;i <= points; ++i) {
    		father[i] = i,cnt[i] = 1;
    		scanf("%d
    ",&src[i]);
    		G[src[i]] = i;
    	}
    	for(int x,y,i = 1;i <= edges; ++i) {
    		scanf("%d%d",&x,&y);
    		int fx = Find(x);
    		int fy = Find(y);
    		if(fx != fy) {
    			father[fy] = fx;
    			cnt[fx] += cnt[fy];
    		}
    	}
    	for(int i = 1;i <= points; ++i) {
    		int fx = Find(i);
    		Insert(tree[fx],src[i]);
    	}
    	cin >> asks;
    	for(int x,y,i = 1;i <= asks; ++i) {
    		scanf("%s%d%d",c,&x,&y);
    		if(c[0] == 'Q') {
    			int fx = Find(x);
    			if(y > cnt[fx])	puts("-1");
    			else	printf("%d
    ",G[Kth(tree[fx],y)]);
    		}
    		else {
    			int fx = Find(x);
    			int fy = Find(y);
    			if(fx != fy) {
    				if(cnt[fy] > cnt[fx])	swap(fx,fy);
    				father[fy] = fx;
    				cnt[fx] += cnt[fy];
    				for(int j = 1;j <= cnt[fy]; ++j) {
    					int temp = Kth(tree[fy],1);
    					Delete(tree[fy],temp);
    					Insert(tree[fx],temp);
    				}
    			}
    		}
    	}
    	return 0;
    }
    
    int Find(int x)
    {
    	if(father[x] == x)	return x;
    	return father[x] = Find(father[x]);
    }
    
    inline void Rotate(Complex *&a,bool dir)
    {
    	Complex *k = a->son[!dir];
    	a->son[!dir] = k->son[dir];
    	k->son[dir] = a;
    	a->Maintain(),k->Maintain();
    	a = k;
    }
    
    inline void Insert(Complex *&a,int x)
    {
    	if(a == NULL) {
    		a = new Complex(x);
    		return ;
    	}
    	int dir = a->Compare(x);
    	if(dir == -1)
    		a->cnt++;
    	else {
    		Insert(a->son[dir],x);
    		if(a->son[dir]->random > a->random)
    			Rotate(a,!dir);
    	}
    	a->Maintain();
    }
    
    void Delete(Complex *&a,int x)
    {
    	int dir = a->Compare(x);
    	if(dir != -1)
    		Delete(a->son[dir],x);
    	else {
    		if(a->cnt > 1)
    			--a->cnt;
    		else {
    			if(a->son[0] == NULL)	a = a->son[1];
    			else if(a->son[1] == NULL)	a = a->son[0];
    			else {
    				bool _dir = a->son[0]->random > a->son[1]->random;
    				Rotate(a,_dir);
    				Delete(a->son[_dir],x);
    			}
    		}
    	}
    	if(a != NULL)	a->Maintain();
    }
    
    int Kth(Complex *a,int k)
    {
    	if(k <= SIZE(a->son[0]))	return Kth(a->son[0],k);
    	k -= SIZE(a->son[0]);
    	if(k <= a->cnt)	return a->val;
    	return Kth(a->son[1],k - a->cnt);
    }
    


  • 相关阅读:
    react系列(二)高阶组件-HOC
    【译】2分钟介绍Rx
    react系列(一)JSX语法、组件概念、生命周期介绍
    react系列(零)安装
    函数节流和函数防抖
    观察者模式和发布订阅模式(下)
    观察者模式和发布订阅模式(上)
    java学习第二天 20207/7
    2020/7/6博客日报Java的开始--pthread的安装
    node.js实现excel导出/exceljs实现导出
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5246546.html
Copyright © 2011-2022 走看看