BZOJ 3673

BZOJ 3674(加强版)

如果每次操作最多只修改一个点的fa[],那么我们可以借助可持久化线段树来O(logn)做到。如果不考虑找fa[]的过程,时空复杂度都是O(logn)。

想要这样就不能加路径压缩,否则要对路径上的点都要改,最好时空复杂度是O(log^2n),但是空间会炸。

合并集合时按秩合并,这样暴力找fa[]的复杂度为O(logn)。

再加上线段树就是O(log^2n)。(当然空间是O(mlogn))

具体:可持久化线段树每个叶子节点储存其fa[x]。每次按秩合并时,在之前树根的基础上新建logn个点,修改对应位置的fa[rt]=new_fa。

按sz启发式合并更好写,而且sz[]还有其它用。3643比按秩合并慢点,3644一样,洛谷上就快些。。

但都是1个log的。


离线做法:操作的版本能构成一棵树。建出树来DFS,进入一个节点时修改,访问完一个节点时撤销修改。


BZOJ 3673:

//6492kb	36ms
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 600000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=2e4+5,M=2e4+5; int n,root[M];
char IN[MAXIN],*SS=IN,*TT=IN;
struct Segment_Tree
{
#define S M*16
#define lson son[x][0]
#define rson son[x][1]
int tot,fa[S],dep[S],son[S][2]; void Build(int &x,int l,int r)
{
x=++tot;
if(l==r) fa[x]=l;
else Build(lson,l,l+r>>1),Build(rson,(l+r>>1)+1,r);
}
void Add(int x,int l,int r,int p)//合并两个dep相同的集合时,使没被合并的那个dep+1
{
if(l==r) return (void)++dep[x];
int m=l+r>>1;
p<=m?Add(lson,l,m,p):Add(rson,m+1,r,p);
}
void Modify(int &x,int y,int l,int r,int pos,int v)//重建一遍 将pos处的fa改为v
{
x=++tot;
if(l==r) {fa[x]=v, dep[x]=dep[y]; return;}
int m=l+r>>1;
if(pos<=m) rson=son[y][1], Modify(lson,son[y][0],l,m,pos,v);
else lson=son[y][0], Modify(rson,son[y][1],m+1,r,pos,v);
}
int Query(int x,int l,int r,int pos)
{
if(l==r) return x;//返回树上一个节点(dep是节点的)
int m=l+r>>1;
return pos<=m?Query(lson,l,m,pos):Query(rson,m+1,r,pos);
}
int Get_fa(int rt,int x)
{
int p=Query(rt,1,n,x);//找到fa[p]==x的树上节点p(x是位置...)
return x==fa[p]?p:Get_fa(rt,fa[p]);
}
void Union(int &rt,int x,int y)
{
int p1=Get_fa(rt,x),p2=Get_fa(rt,y);
if(fa[p1]==fa[p2]) return;
if(dep[p1]>dep[p2]) std::swap(p1,p2);//r1->r2
Modify(rt,rt,1,n,fa[p1],fa[p2]);//fa[r1]=r2 -> fa[fa[p1]]=fa[p2]
if(dep[p1]==dep[p2]) Add(rt,1,n,fa[p2]);
}
}T; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
} int main()
{
n=read(), T.Build(root[0],1,n);
for(int m=read(),i=1,opt; i<=m; ++i)
{
if((opt=read())==1) root[i]=root[i-1], T.Union(root[i],read(),read());
else if(opt==2) root[i]=root[read()];
else root[i]=root[i-1], puts(T.fa[T.Get_fa(root[i],read())]==T.fa[T.Get_fa(root[i],read())]?"1":"0");
}
return 0;
}

BZOJ 3674:

//67820kb	724ms
//为啥我的空间开M*20就RE呢。。不管了反正也是跑到前10了。
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 600000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=2e5+5,M=2e5+5; int n,root[M];
char IN[MAXIN],*SS=IN,*TT=IN;
struct Segment_Tree
{
#define S M*21
#define lson son[x][0]
#define rson son[x][1]
int tot,fa[S],dep[S],son[S][2]; void Build(int &x,int l,int r)
{
x=++tot;
if(l==r) fa[x]=l;
else Build(lson,l,l+r>>1),Build(rson,(l+r>>1)+1,r);
}
void Add(int x,int l,int r,int p)//合并两个dep相同的集合时,使没被合并的那个dep+1
{
if(l==r) return (void)++dep[x];
int m=l+r>>1;
p<=m?Add(lson,l,m,p):Add(rson,m+1,r,p);
}
void Modify(int &x,int y,int l,int r,int pos,int v)//重建一遍 将pos处的fa改为v
{
x=++tot;
if(l==r) {fa[x]=v, dep[x]=dep[y]; return;}
int m=l+r>>1;
if(pos<=m) rson=son[y][1], Modify(lson,son[y][0],l,m,pos,v);
else lson=son[y][0], Modify(rson,son[y][1],m+1,r,pos,v);
}
int Query(int x,int l,int r,int pos)
{
if(l==r) return x;//返回树上一个节点(dep是节点的)
int m=l+r>>1;
return pos<=m?Query(lson,l,m,pos):Query(rson,m+1,r,pos);
}
int Get_fa(int rt,int x)
{
int p=Query(rt,1,n,x);//找到fa[p]==x的树上节点p(x是位置...)
return x==fa[p]?p:Get_fa(rt,fa[p]);
}
void Union(int &rt,int x,int y)
{
int p1=Get_fa(rt,x),p2=Get_fa(rt,y);
if(fa[p1]==fa[p2]) return;
if(dep[p1]>dep[p2]) std::swap(p1,p2);//r1->r2
Modify(rt,rt,1,n,fa[p1],fa[p2]);//fa[r1]=r2 -> fa[fa[p1]]=fa[p2]
if(dep[p1]==dep[p2]) Add(rt,1,n,fa[p2]);
}
}T; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
} int main()
{
n=read(), T.Build(root[0],1,n);
for(int m=read(),i=1,opt,ans=0; i<=m; ++i)
{
if((opt=read())==1) root[i]=root[i-1], T.Union(root[i],read()^ans,read()^ans);
else if(opt==2) root[i]=root[read()^ans];
else root[i]=root[i-1], printf("%d\n",ans=(T.fa[T.Get_fa(root[i],read()^ans)]==T.fa[T.Get_fa(root[i],read()^ans)]));
}
return 0;
}

启发式合并:

//6492kb	56ms
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 600000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=2e4+5,M=2e4+5; int n,root[M];
char IN[MAXIN],*SS=IN,*TT=IN;
struct Segment_Tree
{
#define S M*16
#define lson son[x][0]
#define rson son[x][1]
int tot,fa[S],sz[S],son[S][2]; void Build(int &x,int l,int r)
{
x=++tot;
if(l==r) fa[x]=l, sz[x]=1;
else Build(lson,l,l+r>>1),Build(rson,(l+r>>1)+1,r);
}
void Modify(int &x,int y,int l,int r,int pos,int v)//重建一遍 将pos处的fa改为v
{
x=++tot;
if(l==r) {fa[x]=v, sz[x]=sz[y]; return;}
int m=l+r>>1;
if(pos<=m) rson=son[y][1], Modify(lson,son[y][0],l,m,pos,v);
else lson=son[y][0], Modify(rson,son[y][1],m+1,r,pos,v);
}
int Query(int x,int l,int r,int pos)
{
if(l==r) return x;//返回树上一个节点(dep是节点的)
int m=l+r>>1;
return pos<=m?Query(lson,l,m,pos):Query(rson,m+1,r,pos);
}
int Get_fa(int rt,int x)
{
int p=Query(rt,1,n,x);//找到fa[p]==x的树上节点p(x是位置...)
return x==fa[p]?p:Get_fa(rt,fa[p]);
}
void Union(int &rt,int x,int y)
{
int p1=Get_fa(rt,x),p2=Get_fa(rt,y);
if(fa[p1]==fa[p2]) return;
if(sz[p1]>sz[p2]) std::swap(p1,p2);//r1->r2
sz[p2]+=sz[p1];
Modify(rt,rt,1,n,fa[p1],fa[p2]);//fa[r1]=r2 -> fa[fa[p1]]=fa[p2]
}
}T; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
} int main()
{
n=read(), T.Build(root[0],1,n);
for(int m=read(),i=1,opt; i<=m; ++i)
{
if((opt=read())==1) root[i]=root[i-1], T.Union(root[i],read(),read());
else if(opt==2) root[i]=root[read()];
else root[i]=root[i-1], puts(T.fa[T.Get_fa(root[i],read())]==T.fa[T.Get_fa(root[i],read())]?"1":"0");
}
return 0;
}

BZOJ.3673/3674.可持久化并查集(可持久化线段树 按秩合并/启发式合并)的更多相关文章

  1. 【bzoj4399】魔法少女LJJ 并查集+权值线段树合并

    题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...

  2. [bzoj] 3673 3674 可持久化并查集 || 可持久化数组

    原题 加强版 题意: 可持久化并查集模板-- 题解: 用可持久化线段树维护一个可持久化数组,来记录每一次操作后的状态. 不能用路径压缩,但是要按置合并,使复杂度保证在O(log) #include&l ...

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

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

  4. Bzoj 3673: 可持久化并查集 by zky(主席树+启发式合并)

    3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Description n个集合 m个操作 操作: 1 a b 合并a,b所在集 ...

  5. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  6. 【BZOJ3673/3674】可持久化并查集/可持久化并查集加强版 可持久化线段树

    [BZOJ3674]可持久化并查集加强版 Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了! ...

  7. 2019.01.21 bzoj3674: 可持久化并查集加强版(主席树+并查集)

    传送门 题意:维护可持久化并查集,支持在某个版本连边,回到某个版本,在某个版本 询问连通性. 思路: 我们用主席树维护并查集fafafa数组,由于要查询历史版本,因此不能够用路径压缩. 可以考虑另外一 ...

  8. BZOJ3673 & BZOJ3674 可持续化并查集 【可持续化线段树维护可持续化数组】

    题目描述 n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0 输入格式 输出 ...

  9. bzoj 2733: [HNOI2012]永无乡【并查集+权值线段树】

    bzoj上数组开大会T-- 本来想用set瞎搞的,想了想发现不行 总之就是并查集,每个点开一个动态开点的权值线段树,然后合并的时候把值并在根上,询问的时候找出在根的线段树里找出k小值,看看这个值属于哪 ...

随机推荐

  1. Pycharm使用详解

    Pycharm使用详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 古人云,工欲善其事,必先利其器.想要学好Python且拥有高效的开发效率,这个时候每个程序员都有自己的开发方式 ...

  2. ThreadLocal实现线程范围的共享变量

    一.如何理解线程范围内共享数据 1.static int num=0; 2.线程1访问num变量,并设置为num=2:线程2访问num变量,并设置为num=3: 3.当线程1中对象A.B.C 在访问线 ...

  3. JXL读取,写入Excel

    JXL读取,写入Excel2003 相关阅读:poi 读写excel2003:http://www.cnblogs.com/gavinYang/p/3576739.htmlpoi 读写excel200 ...

  4. 支付宝APP支付,提示代码 ALIN10070

    ALIN10070 此代码时ALI64代码拆分后的细分代码: 代表签名验证失败等相关问题: 如果近期修改过或者续签 过签约协议,也需要更新公私钥.

  5. Codeforces 932 E. Team Work(组合数学)

    http://codeforces.com/contest/932/problem/E 题意:   可以看做 有n种小球,每种小球有无限个,先从中选出x种,再在这x种小球中任选k个小球的方案数 选出的 ...

  6. 前端 ajax 改写登录界面

    SSM 整合项目开发到一个阶段,想慢慢地把前台框架等技术引入进来 突然碰到一个困惑好久的问题: ajax 替换原本 form 表单 post 提交登录: 一直 404 错误,心塞.... 最后发现原来 ...

  7. JavaScript继承详解(二)

    这一章我们将会重点介绍JavaScript中几个重要的属性(this.constructor.prototype), 这些属性对于我们理解如何实现JavaScript中的类和继承起着至关重要的作用. ...

  8. ReactJS -- 初学入门

    <!DOCTYPE html> <html> <head> <script src="build/react.js"></sc ...

  9. 【两分钟视频教程】如何使用myeclipse在mac本机运行iOS配套的服务器

    如何使用myeclipse在mac本机运行iOS配套的服务器  

  10. python爬虫:抓取下载电影文件,合并ts文件为完整视频

    目标网站:https://www.88ys.cc/vod-play-id-58547-src-1-num-1.html 反贪风暴4 对电影进行分析 我们发现,电影是按片段一点点加载出来的,我们分别抓取 ...