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\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 永无乡 平衡树启示式合并的更多相关文章
- HNOI 2012 永无乡
codevs 1477 永无乡 http://codevs.cn/problem/1477/ 2012年湖南湖北省队选拔赛 时间限制: 1 s 空间限制: 128000 KB 题目描述 Des ...
- 解题:HNOI 2012 永无乡
题面 并查集维护连通性,然后暴力启发式合并就完了,记得合并时边DFS边清空数组 #include<cstdio> #include<cstring> #include<a ...
- Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3955 Solved: 2112[Submit][Statu ...
- Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...
- [BZOJ2733][HNOI2010]永无乡 解题报告 启发式合并,线段树合并
好久没更新博客了,前段时间一直都在考试,都没时间些,现在终于有点闲了(cai guai)... 写了一道题,[HNOI2012]永无乡,其实是一道板子题,我发现我写了好多板子题...还是太菜了... ...
- BZOJ2733 永无乡 【splay启发式合并】
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 4190 Solved: 2226 [Submit][Sta ...
- BZOJ2733 [HNOI2012]永无乡 【线段树合并】
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- BZOJ2733 永无乡【splay启发式合并】
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- BZOJ2733: [HNOI2012]永无乡(线段树合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
随机推荐
- 每天一点Swift(五)控制器的生命周期和SizeClass
字数358 阅读19 评论0 喜欢0 初始化init-->awakeFromNib--> prepare a segue --> SB去设置outlets --> viewDi ...
- 设置程序图标-初识IOS
相信我们都很喜欢做出的程序弄成自己的图标. 在创建项目过后显示的页面中,选择App lcons and launch images, 点击App lcons Source 之后的方向符号,把里面的东 ...
- Android实现左右滑动效果
本示例演示在Android中实现图片左右滑动效果. 关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现.接下来 ...
- HTML页面之间跳转与传值(JS代码)
跳转的方法如下: 方法一: window.location.href = "b.html"; 方法二(返回上一个页面,这个应该不算,先放在这): window.history.ba ...
- JS基础函数
函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块 <script> function demo(){ var a = 10; var b = 25; var sum = a + ...
- Katana概述
OWIN owin是web services和framework组件之间的抽象.抽象包括两个核心要素: environment dictionary 这个数据结构存储处理HTTP请求必须的状态和相关的 ...
- C#图像处理(4):图像的剪裁
图像的剪裁方法,从中间剪裁返回剪裁后的左右两页,方法实现如下: /// <summary> /// 图片裁剪,返回左右两页 /// </summary> /// <par ...
- 去空格 whitespaceAndNewlineCharacterSet
http://blog.csdn.net/worn_nest/article/details/10155495
- c++11 auto
auto 关键字指示编译器使用已声明变量的初始化表达式或 lambda 表达式参数来推导其类型. 在大多情况下,建议您使用 auto 关键字(除非您确实需要转换),因为此关键字可提供以下好处: 可靠性 ...
- android布局常用属性记录
android布局常用属性记录 http://blog.csdn.net/xn4545945/article/details/7717086这里有一部分别人总结的其余的: align:对齐 par ...