BZOJ 3123 [SDOI2013] 森林 - 启发式合并 主席树
Description
给你一片森林, 支持两个操作: 查询$x$到$y$的$K$大值, 连接两棵树中的两个点
Solution
对每个节点$x$动态开权值线段树, 表示从$x$到根节点路径上权值出现的次数。
查询时差分即可: $sum[x]+sum[y]-sum[lca]-sum[f[lca]]$
连边时需要启发式合并,将节点数小的接到节点数大的上去, 再遍历小的树, 并更新权值
我刚开始以为testcase是数据组数, TLE我好久,,
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
using namespace std; const int N = 1e5; int lson[N * ], rson[N * ], sum[N * ], root[N];
int n, m, T, a[N], b[N], f[N][], head[N], tot, dep[N];
int father[N], num[N], cnt, nd_num;
int lastans, Case; struct edge {
int nxt, to;
}e[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;
} void add(int u, int v) {
e[++tot].to = v;
e[tot].nxt = head[u];
head[u] = tot;
} int find_anc(int x) {
return father[x] == x ? x : father[x] = find_anc(father[x]);
} int find_lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = ; ~i; --i) if(dep[f[x][i]] >= dep[y])
x = f[x][i];
if(x == y) return x;
for(int i = ; ~i; --i) if(f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][];
} int fd(int x) {
return lower_bound(b + , b + + cnt, x) - b;
} void ins(int &o, int now, int l, int r, int pos) {
o = ++nd_num;
sum[o] = sum[now] + ;
lson[o] = lson[now];
rson[o] = rson[now];
if(l == r) return;
int mid = (l + r) >> ;
if(pos <= mid) ins(lson[o], lson[now], l, mid, pos);
else ins(rson[o], rson[now], mid + , r, pos);
} int query(int x, int y, int lca, int flca, int l, int r, int k) {
if(l == r) return l;
int mid = (l + r) >> , tmp;
if((tmp = sum[lson[x]] + sum[lson[y]] - sum[lson[lca]] - sum[lson[flca]]) >= k) return query(lson[x], lson[y], lson[lca], lson[flca], l, mid, k);
else return query(rson[x], rson[y], rson[lca], rson[flca], mid + , r, k - tmp);
} void dfs(int u) {
dep[u] = dep[f[u][]] + ;
for(int i = ; i <= ; ++i)
f[u][i] = f[f[u][i - ]][i - ];
ins(root[u], root[f[u][]], , cnt, fd(a[u]));
for(int i = head[u]; i; i = e[i].nxt) {
int nt = e[i].to;
if(nt == f[u][]) continue;
f[nt][] = u;
dfs(nt);
}
} int work() {
lastans = ; tot = ;
nd_num = ;
/* memset(root, 0, sizeof(root));
memset(lson, 0, sizeof(lson));
memset(rson, 0, sizeof(rson));
memset(head, 0, sizeof(head));
memset(dep, 0, sizeof(dep));*/
n = rd; m = rd; T = rd;
for(int i = ; i <= n; ++i) b[i] = a[i] = rd;
sort(b + , b + + n);
cnt = unique(b + , b + + n) - b - ;
for(int i = ; i <= n; ++i) father[i] = i, num[i] = ;
for(int i = ; i <= m; ++i) {
int u = rd, v = rd;
int x = find_anc(u), y = find_anc(v);
father[y] = x;
num[x] += num[y];
add(u, v); add(v, u);
}
for(int i = ; i <= n; ++i) if(!dep[i]) dfs(i);
for(int i = ; i <= T; ++i) {
char c = getchar();
while(c != 'Q' && c != 'L') c = getchar();
int u = rd ^ lastans, v = rd ^ lastans;
if(c == 'Q') {
int lca = find_lca(u, v), flca = f[lca][], k = rd ^ lastans;
lastans = query(root[u], root[v], root[lca], root[flca], , cnt, k);
if(lastans > cnt || lastans < ) return printf("F**k,WA\n"), ;
lastans = b[lastans];
printf("%d\n", lastans);
}
else {
int x = find_anc(u), y = find_anc(v);
if(num[x] < num[y]) {
swap(x, y); swap(u, v);
}
father[y] = x;
num[x] += num[y];
f[v][] = u;
add(v, u); add(u, v);
dfs(v);
}
}
return ;
} int main()
{
Case = rd;
work();
}
BZOJ 3123 [SDOI2013] 森林 - 启发式合并 主席树的更多相关文章
- 【BZOJ3123】[SDOI2013] 森林(启发式合并主席树)
点此看题面 大致题意: 给你一片森林,有两种操作:询问两点之间的第\(k\)小点权和在两棵树之间连一条边. 前置技能:树上主席树 做这道题目,我们首先要会树上主席树. 关于树上主席树,这有一道很好的例 ...
- bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)
Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了!ndsf:暴力就可以轻松虐!zky:…… ...
- BZOJ 2733 [HNOI2012]永无乡 - 启发式合并主席树
Description 1: 查询一个集合内的K大值 2: 合并两个集合 Solution 启发式合并主席树板子 Code #include<cstdio> #include<cst ...
- BZOJ 3123: [Sdoi2013]森林 [主席树启发式合并]
3123: [Sdoi2013]森林 题意:一个森林,加边,询问路径上k小值.保证任意时刻是森林 LCT没法搞,树上kth肯定要用树上主席树 加边?启发式合并就好了,小的树dfs重建一下 注意 测试点 ...
- Bzoj 3123: [Sdoi2013]森林(主席树+启发式合并)
3123: [Sdoi2013]森林 Time Limit: 20 Sec Memory Limit: 512 MB Description Input 第一行包含一个正整数testcase,表示当前 ...
- BZOJ3123: [Sdoi2013]森林(启发式合并&主席树)
3123: [Sdoi2013]森林 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4813 Solved: 1420[Submit][Status ...
- bzoj 3123: [Sdoi2013]森林(45分暴力)
3123: [Sdoi2013]森林 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4184 Solved: 1235[Submit][Status ...
- bzoj 3123 [Sdoi2013]森林(主席树,lca,启发式合并)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- bzoj 3123 [Sdoi2013]森林(主席树+启发式合并+LCA)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
随机推荐
- Redux DevTools浏览器插件调试redux
与redux的Devtools模块不同,该工具主要依赖浏览器插件完成.模式也比Devtools简单点. redux-devtools 是一个非常棒的工具,它可以让你实时的监控Redux的状态树的Sto ...
- pandas 常用清洗数据(二)
1. df.head() Here we import pandas using the alias 'pd', then we read in our data. df.head - shows u ...
- 常用官方php版本下载链接
windows 版本 https://windows.php.net/downloads/releases/archives/ https://windows.php.net/download/#ph ...
- 04_web基础(六)之请求转发与重定向
1.交互方式 Web组件之间跳转: 从AServlet 跳转到 BServlet. 三种类型: 1:请求转发(forward) 2:URL重定向(redirect) 3:请求包含(include) 3 ...
- 加载 AssetBundle 的四种方法
[加载 AssetBundle 的四种方法] 1.AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0); 返回AssetBundle ...
- l2-22(重排链表)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805057860517888 题意:给定链表L1->L2- ...
- centos openvpn 安装
安装环境: 系统:centos 6.5 openvpn:openvpn-2.2.1 lzo:lzo-2.09 下载地址:http://www.oberhumer.com/opensource/l ...
- LibreOJ 6277. 数列分块入门 2
题目链接:https://loj.ac/problem/6278 参考博客:https://blog.csdn.net/qq_36038511/article/details/79725027 这题我 ...
- centos6与centos7区别
CentOS 6 vs CentOS 7的不同 (1)桌面系统[CentOS6] GNOME 2.x[CentOS7] GNOME 3.x(GNOME Shell) (2)文件系统[CentOS6 ...
- MenOS
2018-2019-120189224 <庖丁解牛Iinux内核分析>第三周学习总结 MenuOS的构造 下载内核源代码编译内核 cd ~/LinuxKernel/ wget https: ...