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,分别表示节点数.初始边数.操作数 ...
随机推荐
- oracle 连接池参数
后来排查出数据库监听异常,发现是ORA-12519拒绝错误.后来发现是数据的连接池达到的极致. 具体解决方案如下: --首先检查process和session的使用情况,在sqlplus里面查看. S ...
- “2017面向对象程序设计(Java)第十三周学习总结”存在问题的反馈及本周教学安排
“2017面向对象程序设计(Java)第十三周学习总结”存在问题的反馈及本周教学安排1. 图形界面事件处理技术是Java GUI编程核心技术,要求同学们掌握其基本原理和基本编程模型:2. 本周四理论课 ...
- Android Studio: Application Installation Failed
[Android Studio: Application Installation Failed] 参考:http://stackoverflow.com/questions/32718044/and ...
- excel表格输入思想
1.创建工作簿 SXSSFWorkbook wb = new SXSSFWorkbook(); //#设置单元格的垂直居中,水平居中,字体颜色 2.创建sheet Sheet sheet = wb ...
- 第二篇:Jmeter功能概要
一.jmeter工具组成部分: 1.资源生成器:用于生成测试过程中服务器,负载机的资源代码: 2.用户运行器:通常是一个脚本运行引擎,根据脚本的要求模拟指定用户行为,(lr中的controller) ...
- leetcode 树类型题
树的测试框架: // leetcodeTree.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- PAT1103
1103. Integer Factorization (30) 时间限制 1200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- Android 中Application向Activity 传递数值
比如极光注册时获取用户的唯一标示ID需要在登录时进行传递,实现消息的指定用户推送功能 public String id; public String getId() { return id; } pu ...
- gridview空间使用
1.HTML代码 <asp:GridView ID="gv_Info" runat="server" AutoGenerateColumns=" ...
- json 相关知识
一:json标准格式: 标准JSON的合法符号:{(左大括号) }(右大括号) "(双引号) :(冒号) ,(逗号) [(左中括号) ](右中括号) JSON字符串:特殊字符可在 ...