BZOJ 3674 可持久化并查集加强版(主席树变形)
3673: 可持久化并查集 by zky
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 2515 Solved: 1107
[Submit][Status][Discuss]
Description
n个集合 m个操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之后的状态(查询算作操作)
3 a b 询问a,b是否属于同一集合,是则输出1否则输出0
0<n,m<=2*10^4
Input
Output
Sample Input
1 1 2
3 1 2
2 0
3 1 2
2 1
3 1 2
Sample Output
0
1
HINT
Source
题目链接:BZOJ 3674
原来并查集也是可以可持久化的,其思想应该是在于用可持久化数据结构来维护一个前缀记录,由于普通的并查集一开始每一个节点的祖先都是本身,因此要多一个建树的操作,把所有的叶子节点的祖先都处理好,然后这题是并查集,并不需要差分的思想,也就是说更新或者查询只需要用到一个root点,其他的做法跟普通的并查集差不多,详细的可以看黄大牛的博客:http://hzwer.com/3997.html。
代码:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 2e5 + 7;
struct seg
{
int lson, rson;
int pre;
};
seg T[N * 20];
int root[N], tot;
int n; void init()
{
CLR(root, 0);
tot = 0;
}
void build(int &cur, int l, int r)
{
cur = ++tot;
T[cur].lson = T[cur].rson = 0;
if (l == r)
T[cur].pre = l;
else
{
int mid = MID(l, r);
build(T[cur].lson, l, mid);
build(T[cur].rson, mid + 1, r);
}
}
void update(int &cur, int ori, int l, int r, int pos, int pre)
{
cur = ++tot;
T[cur] = T[ori];
if (l == r)
{
T[cur].pre = pre;
return ;
}
else
{
int mid = MID(l, r);
if (pos <= mid)
update(T[cur].lson, T[ori].lson, l, mid, pos, pre);
else
update(T[cur].rson, T[ori].rson, mid + 1, r, pos, pre);
}
}
int query(int k, int l, int r, int pos) //返回子节点位置
{
if (l == r)
return k;
else
{
int mid = MID(l, r);
if (pos <= mid)
return query(T[k].lson, l, mid, pos);
else
return query(T[k].rson, mid + 1, r, pos);
}
}
int Find(int k, int pre)
{
int idx = query(k, 1, n, pre);
if (pre == T[idx].pre)
return pre;
else
return Find(k, T[idx].pre);
}
int main(void)
{
int m, i, a, b, k, ops;
while (~scanf("%d%d", &n, &m))
{
init();
int ans = 0;
build(root[0], 1, n);
for (i = 1; i <= m; ++i)
{
scanf("%d", &ops);
if (ops == 1)
{
scanf("%d%d", &a, &b);
a ^= ans, b ^= ans;
root[i] = root[i - 1];
int fa = Find(root[i], a);
int fb = Find(root[i], b);
if (fa != fb)
update(root[i], root[i - 1], 1, n, fb, fa);
}
else if (ops == 2)
{
scanf("%d", &k);
k ^= ans;
root[i] = root[k];
}
else if (ops == 3)
{
scanf("%d%d", &a, &b);
a ^= ans, b ^= ans;
root[i] = root[i - 1];
int fa = Find(root[i], a);
int fb = Find(root[i], b);
printf("%d\n", ans = (fa == fb));
}
}
}
return 0;
}
BZOJ 3674 可持久化并查集加强版(主席树变形)的更多相关文章
- BZOJ 3674 可持久化并查集加强版(路径压缩版本)
/* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...
- BZOJ 3674 可持久化并查集加强版(按秩合并版本)
/* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...
- bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)
Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了!ndsf:暴力就可以轻松虐!zky:…… ...
- BZOJ 3673 可持久化并查集 by zky && BZOJ 3674 可持久化并查集加强版 可持久化线段树
既然有了可持久化数组,就有可持久化并查集.. 由于上课讲过说是只能按秩合并(但是我也不确定...),所以就先写了按秩合并,相当于是维护fa[]和rk[] getf就是在这棵树中找,直到找到一个点的fa ...
- bzoj 3674 可持久化并查集加强版——可持久化并查集
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3674 用主席树维护 fa[ ] 和 siz[ ] .改 fa[ ] 和改 siz[ ] 都 ...
- BZOJ 3674: 可持久化并查集加强版
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3674 题意:三种操作:(1)合并ab所在集合:(2)查询ab是否在一个集合:(3) ...
- BZOJ 3674 可持久化并查集加强版 可持久化并查集
题目大意:同3673 强制在线 同3673 仅仅只是慢了一些0.0 这道题仅仅写路径压缩比仅仅写启示式合并要快一点点 两个都写就慢的要死0.0 改代码RE的可能是内存不够 #include<cs ...
- 【BZOJ】3673: 可持久化并查集 by zky & 3674: 可持久化并查集加强版(可持久化线段树)
http://www.lydsy.com/JudgeOnline/problem.php?id=3674 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- bzoj 3673&3674 可持久化并查集&加强版(可持久化线段树+启发式合并)
CCZ在2015年8月25日也就是初三暑假要结束的时候就已经能切这种题了%%% 学习了另一种启发式合并的方法,按秩合并,也就是按树的深度合并,实际上是和按树的大小一个道理,但是感觉(至少在这题上)更好 ...
随机推荐
- 【CF1000C】Covered Points Count(离散化+差分)
点此看题面 大致题意: 给出\(n\)条线段,分别求有多少点被覆盖\(1\)次.\(2\)次...\(n\)次. 正常的算法 好吧,这道题目确实有个很简单的贪心做法(只可惜我做的时候没有想到,结果想了 ...
- 制作带复选框的ListView控件
实现效果: 知识运用 ListView控件的GridLines //设置是否在ListView控件中显示网格线 public bool GridLines{get;set} 和CheckBoxes ...
- 安装软件出现缺少vcruntime140dll的解决方法
转自:http://jingyan.baidu.com/article/49711c617e4000fa441b7c92.html 首先下载vc++2015,注意自己系统是32位还是64位的,下载对应 ...
- 配置管理-SpringCloudConfig
1.搭建配置管理服务 添加依赖 <dependencies> <dependency> <groupId>org.springframework.cloud< ...
- ATM-interface-bank
from lib import commonfrom db import db_handler user_logger = common.get_logger('bank') def check_ba ...
- 用dump为什么总会在前面出现/path/debug.php:193
解决方案,在php.ini中的xdebug中加一行代码:xdebug.overload_var_dump=1
- MQTT的学习之Mosquitto发布-订阅(2)
在<MQTT的学习之Mosquitto安装&使用(1)>一文末尾,我已经模拟了发布-订阅模式,只是那时在服务器直接模拟的,并不是java代码模拟的.下面贴出Java代码 1.首先引 ...
- 最短路径:HDU2006-一个人的旅行(多个起点,多个终点)
题目: 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 查找两个链表的第一个交叉结点(Python实现)
题目 给定两个单链表,查找这两个单链表的第一个交叉节点. 例如:链表list_a为:a1→a2→c1→c2→c3,链表list_b为:b1→b2→b3→c1→c2→c3.那么它们第一个交叉结点为c1. ...
- “帮你APP”团队冲刺5
1.整个项目预期的任务量 (任务量 = 所有工作的预期时间)和 目前已经花的时间 (所有记录的 ‘已经花费的时间’),还剩余的时间(所有工作的 ‘剩余时间’) : 所有工作的预期时间:88h 目前已经 ...