BZOJ 3674: 可持久化并查集模板
Code:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
void setIO(string a){ freopen((a+".in").c_str(),"r",stdin); }
#define maxn 100005
int n,m,cur,root[maxn<<1];
struct Node{
int f,siz;
Node(int f=0,int siz=0):f(f),siz(siz){}
};
struct Segment_Tree{
int lson[maxn*100],rson[maxn*100],fa[maxn*100],siz[maxn*100];
int nodes;
void build(int l,int r,int &o){
if(l>r)return;
o=++nodes;
if(l==r) { siz[o]=1,fa[o]=r; return; }
int mid=(l+r)>>1;
build(l,mid,lson[o]), build(mid+1,r,rson[o]);
}
int update(int l,int r,int o,int pos,int ty,int k){
int oo=++nodes;
lson[oo]=lson[o],rson[oo]=rson[o],fa[oo]=fa[o],siz[oo]=siz[o];
if(l==r) {
if(ty==1) siz[oo]=k;
if(ty==0) fa[oo]=k;
return oo;
}
int mid=(l+r)>>1;
if(pos<=mid) lson[oo]=update(l,mid,lson[o],pos,ty,k);
else rson[oo]=update(mid+1,r,rson[o],pos,ty,k);
return oo;
}
Node query(int l,int r,int o,int pos){
if(l==r){ return Node(fa[o],siz[o]); }
int mid=(l+r)>>1;
if(pos<=mid) return query(l,mid,lson[o],pos);
else return query(mid+1,r,rson[o],pos);
}
Node find(int x,int state){
Node p=query(1,n,root[state],x);
return p.f==x?p:find(p.f,state);
}
void merge(int a,int b,int state){
Node x=find(a,state), y=find(b,state);
if(x.f==y.f) return;
if(x.siz>y.siz)
root[cur]=update(1,n,root[state],x.f,1,y.siz+x.siz),root[cur]=update(1,n,root[cur],y.f,0,x.f);
else
root[cur]=update(1,n,root[state],y.f,1,y.siz+x.siz),root[cur]=update(1,n,root[cur],x.f,0,y.f);
}
int ask(int a,int b,int state){
Node x=find(a,state),y=find(b,state);
if(x.f==y.f)return 1;
return 0;
}
}S;
int main(){
//setIO("input");
int opt,a,b;
scanf("%d%d",&n,&m);
S.build(1,n,root[0]);
for(int i=1;i<=m;++i){
scanf("%d",&opt);
cur=i;
if(opt==1) scanf("%d%d",&a,&b),S.merge(a,b,cur-1);
if(opt==2) scanf("%d",&a),root[cur]=root[a];
if(opt==3) scanf("%d%d",&a,&b),root[cur]=root[cur-1], printf("%d\n",S.ask(a,b,cur));
}
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 可持久化并查集加强版(主席树变形)
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Submit: 2515 Solved: 1107 [Submit][Sta ...
- 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数组, 查询时间复杂度为nlognlogn,一个log是并查集 ...
- 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 ...
随机推荐
- CoreData 从入门到精通 (一) 数据模型 + CoreData 栈的创建
CoreData 是 Cocoa 平台上用来管理模型层数据和数据持久化的一个框架,说简单点,就是一个数据库存储框架.CoreData 里相关的概念比较多,而且初始化也非常繁琐,所以对初学者的学习还是有 ...
- matplotlib 可视化 —— matplotlib.patches
官方帮助文档 patches - Matplotlib 1.5.1 documentation patches 下主要包含的常用图形类有: Eclipse Circle Wedge 1. plt.gc ...
- xBIM 基础13 WeXplorer 设置模型颜色
系列目录 [已更新最新开发文章,点击查看详细] 默认情况下模型具有合理的图形表示.这是从IFC模型中获取的,它应该在所有工具中看起来相同,它应该与您或您的用户的创作环境中的相同.但有时候能够改 ...
- 51nod 2020 排序相减(暴力解法)
题目: 代码: #include <bits\stdc++.h> using namespace std; int trim(int x){ ]; ;i < ; i++){ a[i] ...
- 一个PHPer如何深入学习ES搜索引擎?
公司早在一年前就上ES作为后端搜索服务的项目 ,我们PHPer只是负责实现业务接口,es的一些查询,优化技巧由另一组同事(JAVAer)负责,有时,一个需求过来,改动较大时,需要更改查询json语句, ...
- 字符串格式时间转Date格式
/** * 字符串时间格式转 Date 格式 * @param strDate * @return */ public static Date getDateTimeByStringTime(Stri ...
- pyftpdlib 搭建ftp环境
环境搭建: pythonwindows/linuxpip install pyftpdlib (安装失败请到这里下载:https://pypi.python.org/pypi/pyftpdlib/)一 ...
- 在学校机房联想硬盘保护下安装Linux,并配置锐捷客户端
最近几天一直在机房里刷题,空调开着非常舒服.但是机房电脑里全是windows系统,不太好用,挺膈应人的. 一直打算换个系统,刚才终于搞定网络问题了,以后用电脑就可以爽到了. 联想硬盘保护系统下u盘安装 ...
- Centos6.6 系统优化
1:最小化安装 2:修改网卡 vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0HWADDR=52:54:00:0e:c2:c3TYPE ...
- 【转】 C# DEBUG 调试信息打印及输出详解
[转] C# DEBUG 调试信息打印及输出详解 1.debug只在[debug模式下才执行](运行按钮后面的下拉框可选) 2.debug提供了许多调试指令,如断言 System.D ...