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]永无乡 - 启发式合并主席树的更多相关文章

  1. BZOJ 2733: [HNOI2012]永无乡 启发式合并treap

    2733: [HNOI2012]永无乡 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  2. BZOJ 2733 [HNOI2012]永无乡 (权值线段树启发式合并+并查集)

    题意: n<=1e5的图里,在线连边.查询某连通块第k大 思路: 练习线段树合并的好题,因为依然记得上一次启发式合并trie的时候内存爆炸的恐怖,所以这次还是用了动态开点.回收 听说启发式合并s ...

  3. BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)

    不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...

  4. BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]

    2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...

  5. Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3955  Solved: 2112[Submit][Statu ...

  6. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

  7. bzoj2733: [HNOI2012]永无乡 启发式合并

    地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 题目: 2733: [HNOI2012]永无乡 Time Limit: 10 Sec   ...

  8. bzoj 2733: [HNOI2012]永无乡 离线+主席树

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1167  Solved: 607[Submit][Status ...

  9. bzoj 2733: [HNOI2012]永无乡 -- 线段树

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...

随机推荐

  1. tomcat架构分析(valve机制)

    关于tomcat的内部逻辑单元的存储空间已经在相关容器类的blog里阐述了.在每个容器对象里面都有一个pipeline及valve模块. 它们是容器类必须具有的模块.在容器对象生成时自动产生.Pipe ...

  2. Error logged from Ant UI:

    2down vote Have a look at the Eclipse error log. I recently saw following error there: Error logged ...

  3. ABAP 自定义排序的思想(不用系统标准的SORT语句)

    不用ABAP的标准SORT语句,你能将下面这个数组按从小到大(或从大到小)的顺序重新排列,并计算其算法复杂度吗? 现在假设有一个数组:A = [10,66,52,102,-65,85,99,1,56, ...

  4. springboot-day02-整合mybatis与简单使用

    前言:该文章是紧接上一篇文章springboot-day01-引入如何读取配置文件以及helloWorld 1.springboot整合mybatis 1.添加jar包依赖 <?xml vers ...

  5. com.google.gson.stream.MalformedJsonException

    今天下午项目更新后,调用接口时,提示“请求失败”,发现项目网络框架解析时,报错如下:com.google.gson.stream.MalformedJsonException: Unterminate ...

  6. Using Service Workers

    [Using Service Workers] 1.This is an experimental technology Because this technology's specification ...

  7. appium桌面版本以及一些自动化测试方方封装

    appium_desktop 标签(空格分隔): appium_desktop 一 appium_desktop_v1.2.6 1.appium_desktop在github上最新下载地址:appiu ...

  8. appium +ios 判断元素是否存在,排除visible=“false”的数据

    问题 想要判断name=xxx的元素是否存在,存在的话进行点击,结果页面并没有展示我要的元素时也提示找到了元素   原因 ios通过driver.find_element_by_name(“name值 ...

  9. django1.10使用本地静态文件

    django1.10使用本地静态文件方法 本文介绍的静态文件使用,是指启动web站点后,访问静态资源的用法,实际静态资源地址就是一个个的url 如果没有启动web站点,只是本地调试html页面,那直接 ...

  10. oracle取某字符串字段的后4位

    参考 https://zhidao.baidu.com/question/2142799026528780468.html select substr('str1234', -4) from dual