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,每座岛都有自 ...
随机推荐
- CSS 盒子大小
盒子的宽和高 盒子的大小通过宽和高来指定. 默认情况下,盒子的大小刚好容纳其中的内容. 两个属性设置盒子的宽和高 width 设置宽 height 设置高 示例: 1 2 3 4 5 6 7 8 9 ...
- Java中String的intern方法,javap&cfr.jar反编译,javap反编译后二进制指令代码详解,Java8常量池的位置
一个例子 public class TestString{ public static void main(String[] args){ String a = "a"; Stri ...
- UMD编码规范
(function(global, factory){ typeof module === 'object' && typeof module.exports === 'object' ...
- mysql执行流程
https://www.jianshu.com/p/71a98f1347b9 image image SQL示例: SELECT DISTINCT < select_list > ...
- MyBatis数据库连接的基本使用-补充Mapper映射器
补充 Mapper映射器的使用: Mapper映射器,google添加.Mapper映射器是将mapper.xml中配置的sql id,parameterType和resultMap按照规则一一映射到 ...
- 用jQuery实现轮播图效果,js中的排他思想
---恢复内容开始--- jQuery实现轮播图不用单独加载. 思路: a. 通过$("#id名");选择需要的一类标签,获得一个伪数组 b.由于是伪数组的原因,而对数组的处理最多 ...
- cdnbest如何检查https证书是否有效
注意: 用此方法检查ssl证书是否有效,此帐号下必须有有效的cdn节点,因为这个证书是要通过底层的cdn节点来检测的 1. 在站点设置中如下图点打开添加ssl证书 2.加完证书后点检查,打勾就表示证书 ...
- BOM 对象--location、navigator、screen、history
1.location 对象 location提供了与当前窗口中加载的文档有关的信息,还有一些导航功能.需要注意的是,window.location 和 document.location 引用的是同一 ...
- vue 学习1
.static{ border-radius:4px; } .active { width: 100px; height: 100px; background: green; } .text-dang ...
- jvm参考网页
--------------------------------------------- https://www.e-learn.cn/content/wangluowenzhang/37475 h ...