bzoj 3673 可持久化并查集 by zky
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
思路:
#include<bits/stdc++.h>
using namespace std;
#define mid int m = (l + r) >> 1
const int M = 2e6 + ; int sum[M],ls[M],rs[M],dep[M],n,idx,root[M];
void build(int l,int r,int &rt){
rt = ++idx;
if(l == r){
sum[rt] = l;
return ;
}
mid;
build(l,m,ls[rt]); build(m+,r,rs[rt]);
return ;
} void update(int old,int &rt,int p,int c,int l,int r){
rt = ++idx; ls[rt] = ls[old]; rs[rt] = rs[old];
dep[rt] = dep[old];
if(l == r){
sum[rt] = c;
return ;
}
mid;
if(p <= m) update(ls[old],ls[rt],p,c,l,m);
else update(rs[old],rs[rt],p,c,m+,r);
} int query(int p,int l,int r,int rt){
if(l == r) return rt;
mid;
if(p <= m) return query(p,l,m,ls[rt]);
else return query(p,m+,r,rs[rt]);
} void add(int p,int l,int r,int rt){
if(l == r){
dep[rt] ++;
return;
}
mid;
if(p <= m) add(p,l,m,ls[rt]);
else add(p,m+,r,rs[rt]);
} int fd(int x,int rt){
int pos = query(x,,n,rt);
if(x == sum[pos]) return pos;
return fd(sum[pos],rt);
} int main()
{
int q,op,x,y,k;
scanf("%d%d",&n,&q);
build(,n,root[]);
for(int i = ;i <= q;i ++){
scanf("%d",&op);
if(op == ){
scanf("%d%d",&x,&y);
root[i] = root[i-];
int fx = fd(x,root[i-]);
int fy = fd(y,root[i-]);
if(sum[fx] == sum[fy]) continue;
if(dep[fx] > dep[fy]) swap(fx,fy);
update(root[i-],root[i],sum[fx],sum[fy],,n);
if(dep[fx] == dep[fy]) add(sum[fy],,n,root[i]);
}
else if(op == ){
scanf("%d",&k);
root[i] = root[k];
}
else {
root[i] = root[i-];
scanf("%d%d",&x,&y);
int fx = fd(x,root[i]);
int fy = fd(y,root[i]);
if(sum[fx] == sum[fy]) printf("1\n");
else printf("0\n");
}
}
return ;
}
bzoj 3673 可持久化并查集 by zky的更多相关文章
- Bzoj 3673: 可持久化并查集 by zky(主席树+启发式合并)
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Description n个集合 m个操作 操作: 1 a b 合并a,b所在集 ...
- BZOJ 3673 可持久化并查集 by zky && BZOJ 3674 可持久化并查集加强版 可持久化线段树
既然有了可持久化数组,就有可持久化并查集.. 由于上课讲过说是只能按秩合并(但是我也不确定...),所以就先写了按秩合并,相当于是维护fa[]和rk[] getf就是在这棵树中找,直到找到一个点的fa ...
- 3673: 可持久化并查集 by zky
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 2170 Solved: 978[Submit][Status ...
- 【BZOJ】3673: 可持久化并查集 by zky & 3674: 可持久化并查集加强版(可持久化线段树)
http://www.lydsy.com/JudgeOnline/problem.php?id=3674 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)
http://www.lydsy.com/JudgeOnline/problem.php?id=3673 题意: 思路: 可持久化数组可以用可持久化线段树来实现,并查集的查询操作和原来的一般并查集操作 ...
- bzoj 3673 可持久化并查集
本质上是维护两个可持久化数组,用可持久化线段树维护. /************************************************************** Problem: ...
- BZOJ 3674 可持久化并查集加强版(主席树变形)
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Submit: 2515 Solved: 1107 [Submit][Sta ...
- 【BZOJ3673】&&【BZOJ3674】: 可持久化并查集 by zky 可持久化线段树
没什么好说的. 可持久化线段树,叶子节点存放父亲信息,注意可以规定编号小的为父亲. Q:不是很清楚空间开多大,每次询问父亲操作后修改的节点个数是不确定的.. #include<bits/stdc ...
- BZOJ 3674 可持久化并查集加强版(路径压缩版本)
/* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...
随机推荐
- Mysql乱码问题总结
这两天研究了下Mysql的字符集编码和排序规则,有个很典型的问题就是乱码问题.所以小记一下. http://www.jianshu.com/p/4c6a27542df4 http://blog.csd ...
- Python_复习_34
+# 函数 —— 2天 # 函数的定义和调用 # def 函数名(形参): #函数体 #return 返回值 #调用 函数名(实参) # 站在形参的角度上 : 位置参数,*args,默认参数(陷阱), ...
- H5 video标签的第二种格式
36-video标签的第二种格式 2.第二种格式存在的意义: 由于视频数据非常非常的重要, 所以五大浏览器厂商都不愿意支持别人的视频格式, 所以导致了没有一种视频格式是所有浏览器都支持的 这个时候W3 ...
- CentOS 7从Python 2.7升级至Python3.6.1
引言: CentOS是目前最为流行的Linux服务器系统,其默认的Python 2.x,但是根据python社区的规划,在不久之后,整个社区将向Python3迁移,且将不在支持Python2, 那该如 ...
- iOS蓝牙开发之iBeacon技术
iBeacon组成信息: 1 .UUID(universally unique identifier):一个128位的唯一标识一个或多个Beacon基站为特定类型或特定的组织. 2. Major:一个 ...
- XT535
今天金山误删了一个文件,把手机系统整坏了,故刷了个机,刷机教程: http://bbs.dospy.com/thread-15027415-1-623-1.html 中间安装了个驱动精灵,否则手机开启 ...
- debian6保存iptables规则
iptables规则不保存,一旦机器重启规则就清空了,所以需要保存: iptables-save >/etc/iptables-script vi /etc/rc.local 然后在文件中输入: ...
- Jmeter之Constant Timer与constant throughput timer的区别(转)
当放置Constant Timer于两个http请求之间,那么它代表的含义是:在上一个请求发出至完成后, 开始Contant Timer指定的时间,最后再发出第二个请求.它并不是代表两个请求之间的发送 ...
- oracle一些单记录函数
单记录函数 1.0 NVL() 作用:从两个表达式返回一个非NULL值 用法:NVL(表达式1, 表达式2) 如果表达式1的结果不为NULL,返回表达式1的结果:如果表达式1的结果为NULL,返回表达 ...
- Go To Oracle
1.下载mingw (gcc 编译)---win32 2.下载OCI最新版,存放于C:\instantclient_12_1 ---win32 3.下载OCI SDK最新版,存放于C:\ins ...