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 ...
随机推荐
- 【Struts】服务器文件的上传和下载
Java中获得文件的文件后缀 import java.io.*; public class FileTest{ public static void main(String args[]){ File ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU 5882 Balanced Game
Balanced Game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdu 4081 Qin Shi Huang's National Road System(最小生成树+dp)2011 Asia Beijing Regional Contest
同样是看别人题解才明白的 题目大意—— 话说秦始皇统一六国之后,打算修路.他要用n-1条路,将n个城市连接起来,并且使这n-1条路的距离之和最短.最小生成树是不是?不对,还有呢.接着,一个自称徐福的游 ...
- Tombstone crash
首先,android平台应用程序可能产生以下四种crash:App层:Force close crashANR crashNative层:Tombstone crashKernel层:Kernel p ...
- Druid连接池简单入门
偶尔的机会解释Druid连接池,后起之秀,但是评价不错,另外由于是阿里淘宝使用过的所以还是蛮看好的. 1.jar包依赖--Druid依赖代码 <dependency> <groupI ...
- Web Notification
在OS X 10.8 Mountain Lion系统上,通过Safari访问的页面能够发送通知到系统右边栏通知中心,通知(Notification)是通过WebKit Notification 对象发 ...
- G-sensor驱动分析
重力传感器代码分析 重力传感器驱动的功能,主要是向HAL层提供IOCTRL接口,并通过input设备上报数据.芯片实际数据的读取是采用i2c协议读取原始数据,并且作为i2c设备挂载在系统上工作的. 1 ...
- vim对erlang语法支持
发现vim写erlang代码语法缩进都不对,后来发现vim是7.0的,vim7.3开始才对erlang这块进行了支持,所以升级vim git上下载源码包,然后一系列配置安装 http://www.2c ...
- web.py实现jsonp
浏览器端请求 $.getJSON("/currenttime?callback=?", function (json){ $("#time").html(jso ...