BZOJ 2733 [HNOI2012]永无乡 - 启发式合并主席树
Description
1: 查询一个集合内的K大值
2: 合并两个集合
Solution
启发式合并主席树板子
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define per(i,a,b) for(register int i = (a); i >= (b); --i)
using namespace std; const int N = 1e5 + 1e4; int a[N], id[N], b[N];
int root[N], lson[N * ], rson[N * ], sum[N * ];
int n, m, nd_num;
int father[N], num[N]; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} int cmp(int x, int y) {
return a[x] < a[y];
} int find_anc(int x) {
return father[x] == x? x : father[x] = find_anc(father[x]);
} int fd(int x) {
return lower_bound(b + , b + + n, x) - b;
} void ins(int l, int r, int pos, int &nd) {
if(!nd) nd = ++nd_num;
sum[nd]++;
if(l == r) return;
int mid = (l + r) >> ;
if(pos <= mid) ins(l, mid, pos, lson[nd]);
else ins(mid + , r, pos, rson[nd]);
} int query(int l, int r, int k, int nd) {
if(l == r) return l;
int mid = (l + r) >> ;
if(sum[lson[nd]] >= k) return query(l, mid, k, lson[nd]);
else return query(mid + , r, k - sum[lson[nd]], rson[nd]);
} int mg(int l, int r, int x, int y) {
if(!x || !y) return x + y;
int nd = ++nd_num;
sum[nd] = sum[x] + sum[y];
if(l == r) return nd;
int mid = ( l + r) >> ;
lson[nd] = mg(l, mid, lson[x], lson[y]);
rson[nd] = mg(mid + , r, rson[x], rson[y]);
return nd;
} int main()
{
n = rd; m = rd;
rep(i, , n) id[i] = father[i] = i, num[i] = ;
rep(i, , n) b[i] = a[i] = rd;
sort(id + , id + + n, cmp);
sort(b + , b + + n);
rep(i, , n) ins(, n, fd(a[i]), root[i]);
rep(i, , m) {
int u = rd, v = rd;
int x = find_anc(u), y = find_anc(v);
if(num[x] < num[y]) swap(x, y);
father[y] = x;
num[x] += num[y];
root[x] = mg(, n, root[x], root[y]);
}
int T = rd;
for(; T; T--) {
char c = getchar();
while(c != 'Q' && c != 'B') c = getchar();
int u = rd, v = rd;
if(c == 'Q') {
int x = find_anc(u), re;
if(num[x] < v) {puts("-1"); continue;}
re = query(, n, v, root[x]);
printf("%d\n", id[re]);
}
else {
int x = find_anc(u), y = find_anc(v);
if(x == y) continue;
if(num[x] < num[y]) swap(x, y);
father[y] = x;
num[x] += num[y];
root[x] = mg(, n, root[x], root[y]);
}
}
}
BZOJ 2733 [HNOI2012]永无乡 - 启发式合并主席树的更多相关文章
- BZOJ 2733: [HNOI2012]永无乡 启发式合并treap
2733: [HNOI2012]永无乡 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- BZOJ 2733 [HNOI2012]永无乡 (权值线段树启发式合并+并查集)
题意: n<=1e5的图里,在线连边.查询某连通块第k大 思路: 练习线段树合并的好题,因为依然记得上一次启发式合并trie的时候内存爆炸的恐怖,所以这次还是用了动态开点.回收 听说启发式合并s ...
- 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,每座岛都有自 ...
随机推荐
- 微信小程序----搜索框input回车搜索事件
在微信小程序里的搜索框,按软键盘回车键触发搜索事件. <input type="text" placeholder="搜索" value="{ ...
- k8s operator
https://coreos.com/blog/introducing-operators.html Site Reliability Engineer(SRE)是通过编写软件来运行应用程序的人员. ...
- JS-cookie和正则表达式
一 cookie 1 什么是cookie? 会话跟踪技术 2 作用 验证身份,存储信息. 3 特点 大小限制,最多存4k: 每个域下只能存50个cookie: 有时间限制: 只能存放字符串: 只能访问 ...
- EF CodeFirst学习笔记001--主键约定
Code First 的核心是约定,这些默认的规则使我们可以用我们自己的类来创建模型.EF框架要求一个类必须有一个键属性.规则约定如果一个属性名为Id或者是类名+Id的形式(如PatientId),这 ...
- ezmorph将一种对象转换成另外一种对象
EZMorph支持原始数据类型(Primitive),对象(Object),多维数组转换与DynaBeans的转换.兼容JDK1.3.1,整个类库大小只有76K左右. 在Java EE开发常用的str ...
- 理解 IAAS、PAAS、SAAS
引用:http://cloud.51cto.com/art/201802/565858.htm 在与相关人士聊云计算的时候,有时会从他们的最终蹦出诸如IaaS.PaaS和SaaS等相关名词,听的人一头 ...
- CRM销售管理功能
联系项目project:-------是一个大的项目,比如通知开会之类 每个坐席需要分配自己的联系任务,每个联系任务,有自己的完成未完成状态.同时关联着通话记录等 销售计划----销售项目 销售流程: ...
- 正则表达式pattern属性
你发现自己多久匆匆编写一些正则表达式验证一个特定的文本. 多亏了新的pattern属性,我们可以在标签处直接插入一个正则表达式. <form action="" method ...
- set<pair<int,int> >的用法
例题链接:https://vjudge.net/contest/236677#problem/D 题意:首先输入两个数字n,m.n表示有n张桌子,编号从1到n,m表示有m个操作, 然后接下来一行有n个 ...
- PAT L2-013 红色警报(并查集求连通子图)
战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不 ...