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: 可持久化并查集模板的更多相关文章

  1. BZOJ 3674 可持久化并查集加强版(路径压缩版本)

    /* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...

  2. BZOJ 3674 可持久化并查集加强版(按秩合并版本)

    /* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...

  3. BZOJ 3674 可持久化并查集加强版(主席树变形)

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 2515  Solved: 1107 [Submit][Sta ...

  4. bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)

    Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了!ndsf:暴力就可以轻松虐!zky:…… ...

  5. BZOJ 3673 可持久化并查集 by zky && BZOJ 3674 可持久化并查集加强版 可持久化线段树

    既然有了可持久化数组,就有可持久化并查集.. 由于上课讲过说是只能按秩合并(但是我也不确定...),所以就先写了按秩合并,相当于是维护fa[]和rk[] getf就是在这棵树中找,直到找到一个点的fa ...

  6. BZOJ 3674 可持久化并查集

    https://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化数组维护并查集的fa数组, 查询时间复杂度为nlognlogn,一个log是并查集 ...

  7. bzoj 3674 可持久化并查集加强版——可持久化并查集

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3674 用主席树维护 fa[ ]  和 siz[ ] .改 fa[ ] 和改 siz[ ] 都 ...

  8. BZOJ 3674: 可持久化并查集加强版

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3674 题意:三种操作:(1)合并ab所在集合:(2)查询ab是否在一个集合:(3) ...

  9. BZOJ 3674 可持久化并查集加强版 可持久化并查集

    题目大意:同3673 强制在线 同3673 仅仅只是慢了一些0.0 这道题仅仅写路径压缩比仅仅写启示式合并要快一点点 两个都写就慢的要死0.0 改代码RE的可能是内存不够 #include<cs ...

随机推荐

  1. Linux就该这么学 20181005(第九章SSH远程对话)

    参考链接https://www.linuxprobe.com/ nmtui开启网卡设置 ONBOOT=yes systemctl restart network nmcli connection sh ...

  2. Java多线程编程模式实战指南(二):Immutable Object模式--转载

    本文由本人首次发布在infoq中文站上:http://www.infoq.com/cn/articles/java-multithreaded-programming-mode-immutable-o ...

  3. c# iTextSharp导出PDF

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Th ...

  4. 读 Real-Time Rendering 收获 - chapter 6. texturing

    Texturing, at its simplest, is a techinique for efficiently modeling the surface's properties.

  5. 《Unix环境高级编程》读书笔记 第10章-信号

    1.引言 信号是软件中断. 信号提供了一种处理异步事件的方法. 2. 信号概念 信号的名字都是以3个字符SIG开头. Linux3.2.0支持31种信号.FreeBSD.Linux和Solaris作为 ...

  6. 多任务-进程之PID

    1.进程pid,如何在程序中获取我们的进程号,从而查看当前的进程 # -*- coding:utf-8 -*- from multiprocessing import Process import o ...

  7. vue-cli解析

    前言 这段时间,算是空出手来写几篇文章了.由于很久都没有时间整理现在所用的东西了,所以,接下来会慢慢整理出一些文档来记录前段时间的工作和生活. 这篇文章的主题是vue-cli的理解.或许,很多人在开发 ...

  8. 紫书 习题 8-17 UVa 11536 (滑动窗口)

    这道题说连续子序列, 马上就想到滑动窗口. 注意窗口里面的元素中小于等于k的才是有效元素.记录窗口里面有效元素的个数, 满足了之后开始 缩短窗口, 如果左端点不是有效元素或者即使窗口中存在这个元素的个 ...

  9. pointer-events的css属性。使用该属性可以决定是否能穿透绝对定位元素去触发下面元素的某些行为

    pointer-events的css属性.使用该属性可以决定是否能穿透绝对定位元素去触发下面元素的某些行为,比如当一个元素盖住了某个点击事件时可用. 现在Firefox3.6+/Safari4+/Ch ...

  10. 2015 Multi-University Training Contest 2 Buildings

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...