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,每座岛都有自 ...
随机推荐
- 线程执行synchronized同步代码块时再次重入该锁过程中抛异常,是否会释放锁
一个线程执行synchronized同步代码时,再次重入该锁过程中,如果抛出异常,会释放锁吗? 如果锁的计数器为1,抛出异常,会直接释放锁: 那如果锁的计数器为2,抛出异常,会直接释放锁吗? 来简单测 ...
- ASP.NET 三级联动
三级联动就是用三个下拉列表框DropDownList,每个里面添加相应的东西,在第一个列表框中选择一个值,第二三个列表框都会根据第一个选择进行相应的变化,在第二个列表框中选择一个值,第三个列表框也会根 ...
- R语言-画柱形图
barplot()函数 1.柱形图 > sales<-read.csv("citysales.csv",header=TRUE) #读取数据 > barplot( ...
- TensorFlow 语法
dataset = tf.data.TextLineDataset(file_path) 生成一个dataset,dataset中的每一个元素就对应了文件中的一行
- Android Studio: Application Installation Failed
[Android Studio: Application Installation Failed] 参考:http://stackoverflow.com/questions/32718044/and ...
- NPM安装依赖速度慢问题
[NPM安装依赖速度慢问题] npm config set registry http://registry.npm.taobao.org 参考:http://blog.csdn.net/rongbo ...
- Appium+python自动化4-等待函数
4.1 等待函数癈使用 4.1.1 为什么要使用等待函数 我们在做自动化的时候很多时候都不是很顺利,不是因为app的问题,我们的脚本也没问题,但是很多时候都会报错,比如一个页面本来就有id为1的这个元 ...
- DBVIS工具 管理数据库链接
- Jmeter录制APP脚本
启动 jmeter.bat 在 Test Plan 下 添加 Thread Group 在 WorkBench 下 添加 HTTP(S) Test Script Recorder: 配置 Global ...
- Json、JavaBean、String等互转
Json.JavaBean.String等互转 本文介绍简单的Json.JavaBean.String互换(下文JavaBean简称Object对象,这里不是很严谨) 转换关系如下: 其中String ...