BZOJ 3674 可持久化并查集加强版 可持久化线段树
2015-12-30 21:51
447 查看
和BZOJ 3673一样。可以翻前一篇文章。
不过内存问题省去了按大小合并。
Submit: 1165 Solved: 415
[Submit][Status][Discuss]
自从zkysb出了可持久化并查集后……
hzwer:乱写能AC,暴力踩标程
KuribohG:我不路径压缩就过了!
ndsf:暴力就可以轻松虐!
zky:……
n个集合 m个操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之后的状态(查询算作操作)
3 a b 询问a,b是否属于同一集合,是则输出1否则输出0
请注意本题采用强制在线,所给的a,b,k均经过加密,加密方法为x = x xor lastans,lastans的初始值为0
0<n,m<=2*10^5
1 1 2
3 1 2
2 1
3 0 3
2 1
3 1 2
0
1
不过内存问题省去了按大小合并。
#include <cstdio> #include <cassert> #include <algorithm> using namespace std; const int M = 200001; class PersistantArray { struct Node { int v; Node *lc, *rc; } *a[M]; Node pool[M * 60], *C; Node *new_node(Node *lc, Node *rc, int v) { C->lc = lc; C->rc = rc; C->v = v; return C++; } int version, last, n; Node *modify(Node *p, int l, int r, int pos, int val) { int mid = l + r >> 1; if (l == r) return new_node(NULL, NULL, val); else if (pos <= mid) return new_node(modify(p->lc, l, mid, pos, val), p->rc, 0); else return new_node(p->lc, modify(p->rc, mid + 1, r, pos, val), 0); } int get(Node *p, int l, int r, int pos) { int mid = l + r >> 1; if (l == r) return p->v; else if (pos <= mid) return get(p->lc, l, mid, pos); else return get(p->rc, mid + 1, r, pos); } public: PersistantArray(int initial_capacity, int initial_data) : C(pool), version(0), last(0), n(initial_capacity) { a[0] = C++; a[0]->lc = a[0]->rc = a[0]; a[0]->v = initial_data; } void roll(int new_version) { version = new_version; } int latest_version() { return last; } int get(int x) { return get(a[version], 1, n, x); } void set(int x, int y) { a[++last] = modify(a[version], 1, n, x, y); } void set(int x, int y, bool) { modify(a[version], 1, n, x, y); } } *fa; int now = 0, ver[M]; int find(int x) { int fx = fa->get(x); if (!fx) return x; int ans = find(fx); fa->set(x, ans, false); return ans; } void merge(int x, int y) { x = find(x), y = find(y); if (x == y) return; fa->set(y, x); now = fa->latest_version(); } int main() { int i, p, x, y, n, m, ans = 0; scanf("%d%d", &n, &m); fa = new PersistantArray(n, 0); for (int i = 1; i <= m; i++) { scanf("%d", &p); fa->roll(now); switch (p) { case 1: scanf("%d%d", &x, &y); merge(x ^ ans, y ^ ans); break; case 2: scanf("%d", &x); now = ver[x ^ ans]; break; case 3: scanf("%d%d", &x, &y); printf("%d\n", ans = (find(x ^ ans) == find(y ^ ans))); break; } ver[i] = now; } return 0; }
3674: 可持久化并查集加强版
Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 1165 Solved: 415
[Submit][Status][Discuss]
Description
Description:自从zkysb出了可持久化并查集后……
hzwer:乱写能AC,暴力踩标程
KuribohG:我不路径压缩就过了!
ndsf:暴力就可以轻松虐!
zky:……
n个集合 m个操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之后的状态(查询算作操作)
3 a b 询问a,b是否属于同一集合,是则输出1否则输出0
请注意本题采用强制在线,所给的a,b,k均经过加密,加密方法为x = x xor lastans,lastans的初始值为0
0<n,m<=2*10^5
Input
Output
Sample Input
5 61 1 2
3 1 2
2 1
3 0 3
2 1
3 1 2
Sample Output
10
1
相关文章推荐
- 近几年前端技术盘点以及 2016 年技术发展方向
- 100. Same Tree
- Linux别名与常用快捷键
- [OC] UITabBarController
- C++11实现一个自动注册的工厂
- RXAndroid Single的使用
- Hackerearth.com编程问题解题思路系列:Roy's Chocolates
- 安卓apk下载安装
- SystemUI下拉通知栏的源码分析
- CDOJ 1269 ZhangYu Speech 数组处理
- stdlib.h
- javaWeb之Servlet的工作原理与生命周期
- ftrace
- LNMP 1.6 常见的502问题解决
- Linux入门导读
- 模拟退火算法
- bzoj4008: [HNOI2015]亚瑟王【期望dp】
- 几种元素的用法
- 实习电面小记
- byte[]转为string,再用substring取值,在eclipse和exe里取得值不同