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 ...
随机推荐
- ThinkPHP5.0框架开发--第9章 TP5.0视图和模板
ThinkPHP5.0框架开发--第9章 TP5.0视图和模板 第9章 TP5.0视图和模板 ===================================================== ...
- CentOS 6.5 下编译安装 Nginx 1.8.0
转自:https://i.cnblogs.com/EditPosts.aspx?postid=8303227&update=1 安装编译依赖的包 yum -y install gcc gcc- ...
- 无法连接虚拟设备 ide1:0,因为主机上没有相应的设备。您要在每次开启此虚拟机时都尝试连接此虚拟设备吗?
转自:http://blog.51cto.com/thawliu/1704876 安装虚拟机时出现提示:无法连接虚拟设备 ide1:0,因为主机上没有相应的设备.您要在每次开启此虚拟机时都尝试连接此虚 ...
- POJ 3044单调栈
题意: 思路: 单调栈 // by SiriusRen #include <stack> #include <cstdio> using namespace std; stac ...
- 新型查询系统impala
这羊头很酷... Apache Impala是Apache Hadoop的开源本地分析数据库.Impala由Cloudera,MapR,Oracle和Amazon提供. 在Hadoop上进行BI风格的 ...
- Codeforces 987A. Infinity Gauntlet(手速题,map存一下输出即可)
解法: 1.先将对应的字符串存入map. 2.然后将输入的串的second置为空. 3.输出6-n,输出map中的非空串. 代码: #include <bits/stdc++.h> usi ...
- hiho150周 - 动态规划*
题目链接 一个n*m的迷宫由‘.’和'b'组成,从(1,1)走到(n,m),只能向右或者向下走,但遇到‘b’时才能改变方向,开始时方向向右. 问到达(n,m)至少改变几个位置上的值 /******** ...
- rman参数
rman 参数 RMAN> show all; 参数是存放在控制文件中的 改参数:(直接改) eg: CONFIGURE RETENTION POLICY TO REDUNDANCY 3 参数: ...
- swift语言点评五-Function
一.函数类型 Every function in Swift has a type, consisting of the function’s parameter types and return t ...
- Dispatch Groups
Dispatch Groups are objects that allow several tasks to be grouped for later joining. Tasks can be a ...