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 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
随机推荐
- HOG(方向梯度直方图)
结合这周看的论文,我对这周研究的Histogram of oriented gradients(HOG)谈谈自己的理解: HOG descriptors 是应用在计算机视觉和图像处理领域,用于目标检測 ...
- C++11中正則表達式測试
VC++2010已经支持regex了, 能够用来编译下述代码. #include <string> #include <regex> #include <iostream ...
- java实验7-多线程编程
1 利用Thread和Runnable创建线程 [实验目的] (1)理解用实现Runnable接口的方法实现多线程. (2)掌握线程优先级的设置. (3)加深对线程状态转换的理解. [实验要求] 要求 ...
- int.Tryparse() 、int.parse()、Convert.To32() 的区别
int.Tryparse() Int32.TryParse(source, result)则无论如何都不抛出异常,只会返回true或false来说明解析是否成功,如果解析失败,调用方将会得到0值. ...
- 2、shell命令学习
1.第一个例子 touch test.sh vim test.sh #!/bin/bash echo "hello world" chmod 755 test.sh ./test. ...
- 对面向对象程序设计(OOP)的认识
前言 本文主要介绍面向对象(OO)程序设计,以维基百科的解释: 面向对象程序设计(英语:Object-oriented programming,缩写:OOP),指一种程序设计范型,同时也是一种程序开发 ...
- js prototype之诡异
想必经常写js的人必然会经常性的用到prototype这个属性,我写这篇文章倒不是自己对prototype这个属性理解有多深刻,相反是因为自己理解肤浅,想通过写文章来加深理解.废话不多说.下面总结一下 ...
- Angry Professor
def main(): t = int(raw_input()) for _ in range(t): n, k = map(int, raw_input().strip().split(' ')) ...
- python文件_批量改名
#! /usr/bin/env python #coding=gbk #文件操作实例--将文件夹下所有图片名称加上'_test' import re,os,time #str.split(path) ...
- 深入理解Azure自动扩展集VMSS(3)
在实际使用过程当中,使用VMSS有一些最佳实践的建议和限制,便于你在做自动扩展设计的时候进行考虑: 关于VMSS 如果你使用的是系统镜像,一个扩展集中虚拟机数量不能超过100 无论是在ASM还是ARM ...