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

后来也有不断的无向边增加,每个岛屿有个一独一无二的重要度,问随意时刻的与一个岛屿联通的全部岛中重要度第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\n",&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\n",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);
}

BZOJ 2733 HNOI 2012 永无乡 平衡树启示式合并的更多相关文章

  1. HNOI 2012 永无乡

    codevs 1477 永无乡 http://codevs.cn/problem/1477/ 2012年湖南湖北省队选拔赛  时间限制: 1 s  空间限制: 128000 KB   题目描述 Des ...

  2. 解题:HNOI 2012 永无乡

    题面 并查集维护连通性,然后暴力启发式合并就完了,记得合并时边DFS边清空数组 #include<cstdio> #include<cstring> #include<a ...

  3. Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3955  Solved: 2112[Submit][Statu ...

  4. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

  5. [BZOJ2733][HNOI2010]永无乡 解题报告 启发式合并,线段树合并

    好久没更新博客了,前段时间一直都在考试,都没时间些,现在终于有点闲了(cai guai)... 写了一道题,[HNOI2012]永无乡,其实是一道板子题,我发现我写了好多板子题...还是太菜了... ...

  6. BZOJ2733 永无乡 【splay启发式合并】

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 4190  Solved: 2226 [Submit][Sta ...

  7. BZOJ2733 [HNOI2012]永无乡 【线段树合并】

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. BZOJ2733 永无乡【splay启发式合并】

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. BZOJ2733: [HNOI2012]永无乡(线段树合并)

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

随机推荐

  1. Linux 运维笔记

    #配置静态地址网卡DEVICE="eth0"BOOTPROTO=staticHWADDR="00:0C:29:DC:EA:F7"NM_CONTROLLED=&q ...

  2. AIX 常用命令和知识

      BOOTLIST:#bootlist -m normal -o (查看bootlist)#bootlist -m normal (设置bootlist为空,谁要在我机器上执行我就要哭了)#boot ...

  3. SpringTest2

    Spring 框架第二天 AOP切面编程 今天重点内容: 1. 什么是AOP ? AOP实现原理是怎样的? AOP相关术语 2. AOP底层实现 (了解) ----- JDK动态代理. Cglib动态 ...

  4. resin安装和配置

    1 从 http://www.caucho.com/download/ 下载resin 2 检查你的linux环境,查看是否安装了 jdk1.5 或以上版本,是否安装了perl. 输入命令:Java ...

  5. 使用date转换UNIX时间戳

    1.将time string转换成时间戳 date -d "2010-10-12 12:25:00" +%s 2.将时间戳转换成time string date -d " ...

  6. 《第一行代码》学习笔记14-UI(3)

    1. (1)所有控件都是直接或间接继承自View,所用的所有布局都是直接或间接继承自ViewGroup的. (2)View是Android中一种最基本的UI组件,可以在屏幕上绘制一块矩形区域,并能响应 ...

  7. iOS_SN_Socket网络编程(一)

    1.Socket简介 首先让我们通过一张图知道socket在哪里? socket在哪里 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口. 2.TCP和UDP的区别 在这里就 ...

  8. [总结]Map: C++ V.S. Java

    整理一下Map在Java 和 C++的基本操作,欢迎大家一起交流学习. 附: 对于在C++中,选用map 还是 unordered_map,可以参考这篇讨论.相对简单粗暴的结论是,unordered_ ...

  9. lunix安装jdk(rpm格式)

    1.下载后,首先把jdk-7u3-linux-x64.rpm复制到/usr/local/src#cp jdk-7u3-linux-x64.rpm /usr/local/src/2.给所有用户添加可执行 ...

  10. 愉快的开始 - Windows程序设计(SDK)000

    愉快的开始 让编程改变世界 Change the world by program  参考教材 购买链接:Windows程序设计(第5版)(珍藏版)(附CD-ROM光盘1张)  学习环境 视频演示:W ...