BZOJ 2733: [HNOI2012]永无乡 启发式合并treap
2733: [HNOI2012]永无乡
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://www.lydsy.com/JudgeOnline/problem.php?id=2733
Description
永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示。某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛。如果从岛 a 出发经过若干座(含 0 座)桥可以到达岛 b,则称岛 a 和岛 b 是连 通的。现在有两种操作:B x y 表示在岛 x 与岛 y 之间修建一座新桥。Q x k 表示询问当前与岛 x连通的所有岛中第 k 重要的是哪座岛,即所有与岛 x 连通的岛中重要度排名第 k 小的岛是哪 座,请你输出那个岛的编号。
Input
输入文件第一行是用空格隔开的两个正整数 n 和 m,分别 表示岛的个数以及一开始存在的桥数。接下来的一行是用空格隔开的 n 个数,依次描述从岛 1 到岛 n 的重要度排名。随后的 m 行每行是用空格隔开的两个正整数 ai 和 bi,表示一开始就存 在一座连接岛 ai 和岛 bi 的桥。后面剩下的部分描述操作,该部分的第一行是一个正整数 q, 表示一共有 q 个操作,接下来的 q 行依次描述每个操作,操作的格式如上所述,以大写字母 Q 或B 开始,后面跟两个不超过 n 的正整数,字母与数字以及两个数字之间用空格隔开。 对于 20%的数据 n≤1000,q≤1000
对于 100%的数据 n≤100000,m≤n,q≤300000
Output
对于每个 Q x k 操作都要依次输出一行,其中包含一个整数,表 示所询问岛屿的编号。如果该岛屿不存在,则输出-1。
Sample Input
5 1
4 3 2 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3
Sample Output
-1
2
5
1
2
HINT
题意
题解:
启发式合并treap,直接套版咯。。。
合并就用并查集来维护,如果不在的话,就强行把小的treap直接暴力insert到大的treap里面就好了
然后再找第k大就好了
代码
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std; #define maxn 100005
int n,m,q;
//////////////////////////////////////////////
struct TreeNode {
TreeNode *L, *R;
int num;
int size;
int pri;
int key;
};
TreeNode nodes[maxn*], stack[maxn*];
TreeNode *null;
int C, Top; void init() {
C = ; Top = ;
null = &nodes[C++];
null->L = null->R = null;
null->pri = -0x7FFFFFFF;
null->size = ;
null->key = ;
null->num = ;
} struct Treap { TreeNode* root;
vector<TreeNode*> list; void init() {
root = null;
} void toArrayList(TreeNode* root) {
if (root != null) {
toArrayList(root->L);
list.push_back(root);
toArrayList(root->R);
}
} TreeNode* newNode(int key, int num) {
TreeNode* ret;
if (Top)
ret = &stack[--Top];
else ret = &nodes[++C];
ret->L = ret->R = null;
ret->key = key;
ret->pri = rand();
ret->num = num;
ret->size = num;
return ret;
} TreeNode* merge(TreeNode* A , TreeNode* B) {//合并
if (A == null)
return B;
if (B == null)
return A;
if (A->pri < B->pri) {
A->R = merge(A->R, B);
pushUp(A);
return A;
} else {
B->L = merge(A, B->L);
pushUp(B);
return B;
}
} pair<TreeNode*, TreeNode*> split(TreeNode* root, int key) {//分裂
pair<TreeNode*, TreeNode*> tmp;
if (root == null) {
tmp = make_pair(null, null);
return tmp;
}
if (key <= root->key) {
tmp = split(root->L, key);
root->L = tmp.second;
pushUp(root);
tmp.second = root;
return tmp;
} else {
tmp = split(root->R, key);
root->R = tmp.first;
pushUp(root);
tmp.first = root;
return tmp;
}
} TreeNode* upperBound(TreeNode* root, int key) {
TreeNode* ans = null;
TreeNode* i = root;
while (i != null) {
if (key < i->key) {
ans = i;
i = i->L;
} else {
i = i->R;
}
}
return ans;
} TreeNode* lowerBound(TreeNode* root, int key) {
TreeNode* ans = null;
TreeNode* i = root;
while (i != null) {
if (key < i->key) {
i = i->L;
} else {
ans = i;
i = i->R;
}
}
return ans;
} bool contains(TreeNode* root, int key) {
if (root == null)
return false;
if (key == root->key)
return true;
else if (key < root->key)
return contains(root->L, key);
else
return contains(root->R, key);
} void insert(TreeNode* root, int key, int num) {
if (key < root->key)
insert(root->L, key, num);
else if (key == root->key)
root->num += num;
else
insert(root->R, key, num);
pushUp(root);
} void insert(int key, int num) {
if (contains(root, key)) {
insert(root, key, num);
return ;
}
pair<TreeNode*, TreeNode*> tmp = split(root, key);
TreeNode* node = newNode(key, num);
root = merge(merge(tmp.first, node), tmp.second);
} void del(int key) {
pair<TreeNode*, TreeNode*> tmp = split(root, key);
TreeNode* node = upperBound(tmp.second, key);
if (node == null)
root = tmp.first;
else
root = merge(tmp.first, split(tmp.second, node->key).second);
} void del(TreeNode* root, int key) {
if (!contains(root, key))
return ;
if (key < root->key)
del(root->L, key);
else if (key == root->key) {
root->num--;
if (root->num == )
del(key);
} else {
del(root->R, key);
}
pushUp(root);
} TreeNode* select(TreeNode* root, int k) {
if (k <= root->L->size)
return select(root->L, k);
else if (k >= root->L->size + && k <= root->L->size + root->num)
return root;
else
return select(root->R, k-root->L->size-root->num);
} int getMin(TreeNode* root, int key) {
if (key < root->key)
return getMin(root->L, key);
else if (key == root->key)
return root->L->size + root->num;
else
return root->L->size + root->num + getMin(root->R, key);
} int getMax(TreeNode* root, int key) {
if (key < root->key)
return root->R->size + root->num + getMax(root->L, key);
else if (key == root->key)
return root->R->size + root->num;
else
return getMax(root->R, key);
} int size() {
return root->size;
} void visit(TreeNode* root) {
if (root != null) {
printf("key: %d num: %d size: %d\n", root->key, root->num, root->size);
visit(root->L);
visit(root->R);
}
} void pushUp(TreeNode* x) {
x->size = x->L->size + x->R->size + x->num;
}
}; //////////////////////////////////////
int fa[maxn];
Treap tr[maxn];
int val[maxn],Hash[maxn];
int fi(int x)
{
if(x!=fa[x])fa[x]=fi(fa[x]);
return fa[x];
}
void uni(int x,int y)
{
x = fi(x),y = fi(y);
if(x!=y)
{
if(tr[x].size()>tr[y].size())
swap(x,y);
fa[x]=y;
tr[x].list.clear();
tr[x].toArrayList(tr[x].root);
for(int i=;i<tr[x].list.size();i++)
{
TreeNode* tmp = tr[x].list[i];
tr[y].insert(tmp->key,tmp->num);
}
}
}
int query(int x,int k)
{
x = fi(x);
if(tr[x].size()<k)return -;
else return tr[x].select(tr[x].root,k)->key;
}
int main()
{
init();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
tr[i].init();
fa[i]=i;
scanf("%d",&val[i]);
Hash[val[i]]=i;
tr[i].insert(val[i],);
}
for(int i=;i<=m;i++)
{
int x,y;scanf("%d%d",&x,&y);
uni(x,y);
}
scanf("%d",&q);
for(int i=;i<q;i++)
{
char s[];
scanf("%s",&s);
int x,y;scanf("%d%d",&x,&y);
if(s[]=='B')
{
uni(x,y);
}
else
{
int p = query(x,y);
if(p==-)printf("-1\n");
else printf("%d\n",Hash[p]);
}
}
}
BZOJ 2733: [HNOI2012]永无乡 启发式合并treap的更多相关文章
- BZOJ 2733 [HNOI2012]永无乡 - 启发式合并主席树
Description 1: 查询一个集合内的K大值 2: 合并两个集合 Solution 启发式合并主席树板子 Code #include<cstdio> #include<cst ...
- BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)
不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...
- BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]
2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...
- 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: [HNOI2012]永无乡 启发式合并
地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 题目: 2733: [HNOI2012]永无乡 Time Limit: 10 Sec ...
- bzoj 2733: [HNOI2012]永无乡 离线+主席树
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1167 Solved: 607[Submit][Status ...
- bzoj 2733: [HNOI2012]永无乡 -- 线段树
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...
- 线段树合并+并查集 || BZOJ 2733: [HNOI2012]永无乡 || Luogu P3224 [HNOI2012]永无乡
题面:P3224 [HNOI2012]永无乡 题解: 随便写写 代码: #include<cstdio> #include<cstring> #include<iostr ...
随机推荐
- CentOS SVN服务器安装配置小记
SVN的安装 安装很简单,尤其对于CentOS这种,直接: # yum install subversion# yum install mod_dav_svn 不同发行版的Package安装方法参见h ...
- Android手动画柱状图的例子
效果图如上,网上看到的例子,谨以此文记录一下,以后用到的地方再来翻翻. 核心技术是用Canvas和Paint画长方形. 源码地址:http://download.csdn.net/detail/abc ...
- Mybatis学习——基本增删改查(CRUD)
Eclipse+Mybatis+MySql 1.所需jar 2.项目目录 3.源代码 package com.zhengbin.entity; public class Student { priva ...
- 【JSP】JSP检查字符串是否为数字
//判断是否是正整数 function IsNum(s) { if(s!=null){ var r,re; re = /\d*/i; //\d表示数字,*表示匹配多个数字 r = s.match(re ...
- css清除浮动的两种方式(clearfix和clear)
最近总是在用浮动,这两种方式总是浮现在眼前,或者说去掉父级和同级浮动样式总在思考中.两种方式怎么写都在base.css中. 在做瑞祥之旅的过程中,还是吃了一个大亏,就是清除浮动,不管是同级还是父级,都 ...
- centos软件环境
1,保持能链接外网和yum的可用性. 注意:yum配置项中最好:keepcache=1 2,yum install gcc, gcc-c++, make, cmake, 3, ntfs-3g wget ...
- 为SQL表添加全文索引范例
--范例: --为HR_Job中的JobTitle,JobDes创建全文索引 execute sp_fulltext_catalog 'boli188', 'create' --创建全文目录,boli ...
- .NET之美——1.1 C#中的泛型
1.1 C#中的泛型 .Net 1.1版本最受诟病的一个缺陷就是没有提供对泛型的支持.通过使用泛型,我们可以极大地提高代码的重用度,同时还可以获得强类型的支持,避免了隐式的装箱.拆箱,在一定程度上提升 ...
- DBHelper (支持事务与数据库变更) z
using System; using System.Data; using System.Data.Common; using Project.BaseFramework; using System ...
- asp.net MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction区别
@Html.Action:需要有对应的Action,并且Action方法有返回值.(注:处理完业务逻辑同时,也需要返回所需值) @{Html.RenderAction}:需要有对应的Action,Ac ...