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. VFS: Cannot open root device "nfs" or unknown-block(0,255)错误的解决

    1. 解决办法:在内核配置时候文件系统中选中Root file system on NFS

  2. redis 存储java对象 两种方式

    根据redis的存储原理,Redis的key和value都支持二进制安全的字符串 1.利用序列化和反序列化的方式存储java对象我们可以通过对象的序列化与反序列化完成存储于取出,这样就可以使用redi ...

  3. MyBatis数据库连接的基本使用

    MyBatis部分: 本部分内容只主要体现Mybatis的特点. (1)MyBatis是什么? 开源的持久层框架,MyBatis的底层仍然是JDBC (2)编程步骤 step1 Maven项目 pom ...

  4. Laravel5.1 与 Laypage 结合进行分页

    demo地址:http://lara.ytlwin.top/orm 路由 Route::match(array('get','post'),'/orm','StuController@orm'); 控 ...

  5. SpringMVC包括哪些组件

      1 映射器 1.1作用:Handlermapping根据url查找Handler   2 适配器 2.1作用:HandlerAdapter执行Handler   3 解析器 3.1作用:View ...

  6. 四:python 对象类型详解一:数字(下)

    一:位操作 除了一般的数学运算,python也支持c语言中的大多数数学表达式.这包括那些把整数当作二进制位串对待的操作.例如,还可以实现位移及布尔操作: >>> x = 1 > ...

  7. JAVA jar 参数

    -client       to select the "client" VM    -server       to select the "server" ...

  8. DBCP连接数据库了解一下

    ---恢复内容开始--- package com.kevin.Utils; import javax.sql.DataSource; import org.apache.commons.dbcp.Ba ...

  9. CentOS7中编译安装redis5.0

    1. 环境介绍 CentOS7 (未安装Development Tools) 2. 下载Redis5.0-rc3 wget -O redis-5.0-rc3.tar.gz https://github ...

  10. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛----随手记录帖

    这是跟学长学姐组队来打的最爽的一次比赛了,也可能是互相组队最后一次比赛了,南哥和楼学姐,省赛之后就退役了,祝他们能考研和面试都有happy ending! 虽然最后没有把F题的n^2约数的数学题写完, ...