【BZOJ 3674】可持久化并查集加强版&【BZOJ 3673】可持久化并查集 by zky 用可持久化线段树破之
最后还是去掉异或顺手A了3673,,,
并查集其实就是fa数组,我们只需要维护这个fa数组,用可持久化线段树就行啦
1:判断是否属于同一集合,我加了路径压缩。
2:直接把跟的值指向root[k]的值破之。
3:输出判断即可。
难者不会,会者不难,1h前我还在膜这道题,现在吗hhh就当支持下zky学长出的题了。
3673:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define read(x) x=getint()
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
using namespace std;
inline const int getint(){char c=getchar();int k=1,r=0;for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
const int N=2*1E4+10;
struct node{int l,r,f;}T[N*100];
int cnt,root[N],n,m;
inline void update(int l,int r,int &pos,int x,int fa){
T[++cnt]=T[pos]; pos=cnt;
if (l==r) {T[pos].f=fa; return;}
int mid=(l+r)>>1;
if (x<=mid) update(l,mid,T[pos].l,x,fa);
else update(mid+1,r,T[pos].r,x,fa);
}
inline int ask(int l,int r,int pos,int x){
if (l==r) return T[pos].f;
int mid=(l+r)>>1;
if (x<=mid) return ask(l,mid,T[pos].l,x);
else return ask(mid+1,r,T[pos].r,x);
}
inline int un(int &rt,int a,int b){
update(1,n,rt,a,b);
return b;
}
inline int find(int &rt,int y){
int f=ask(1,n,rt,y);
return f==y?y:un(rt,y,find(rt,f));
}
int main(){
read(n); read(m); int fx,fy,x,y,c;
for1(i,1,n) update(1,n,root[0],i,i);
for1(i,1,m){
read(c);
root[i]=root[i-1];
switch (c){
case 1:
read(x); read(y);
fx=find(root[i],x),fy=find(root[i],y);
if (fx!=fy) un(root[i],fx,fy);
break;
case 2:
root[i]=root[getint()];
break;
case 3:
read(x); read(y);
fx=find(root[i],x),fy=find(root[i],y);
printf("%d\n",(fx==fy));
break;
}
}return 0;
}
3674:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define read(x) x=getint()
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
using namespace std;
inline const int getint(){char c=getchar();int k=1,r=0;for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
const int N=2*1E5+10;
struct node{int l,r,f;}T[N*100];
int cnt,root[N],n,m;
inline void update(int l,int r,int &pos,int x,int fa){
T[++cnt]=T[pos]; pos=cnt;
if (l==r) {T[pos].f=fa; return;}
int mid=(l+r)>>1;
if (x<=mid) update(l,mid,T[pos].l,x,fa);
else update(mid+1,r,T[pos].r,x,fa);
}
inline int ask(int l,int r,int pos,int x){
if (l==r) return T[pos].f;
int mid=(l+r)>>1;
if (x<=mid) return ask(l,mid,T[pos].l,x);
else return ask(mid+1,r,T[pos].r,x);
}
inline int un(int &rt,int a,int b){
update(1,n,rt,a,b);
return b;
}
inline int find(int &rt,int y){
int f=ask(1,n,rt,y);
return f==y?y:un(rt,y,find(rt,f));
}
int main(){
read(n); read(m); int fx,fy,last=0,x,y,c;
for1(i,1,n) update(1,n,root[0],i,i);
for1(i,1,m){
read(c);
root[i]=root[i-1];
switch (c){
case 1:
read(x); read(y); x^=last; y^=last;
fx=find(root[i],x),fy=find(root[i],y);
if (fx!=fy) un(root[i],fx,fy);
break;
case 2:
root[i]=root[getint()^last];
break;
case 3:
read(x); read(y); x^=last; y^=last;
fx=find(root[i],x),fy=find(root[i],y);
printf("%d\n",last=(fx==fy));
break;
}
}return 0;
}
这样就完了,然而只是达神几年前就随手虐的东西,本蒟蒻必须得努力啊!!!
【BZOJ 3674】可持久化并查集加强版&【BZOJ 3673】可持久化并查集 by zky 用可持久化线段树破之的更多相关文章
- 【BZOJ4388】JOI2012 invitation 堆+线段树+并查集(模拟Prim)
[BZOJ4388]JOI2012 invitation Description 澳洲猴举办了一场宴会,他想要邀请A个男生和B个女生参加,这A个男生从1到A编号,女生也从1到B编号.现在澳洲猴知道n组 ...
- 【bzoj4399】魔法少女LJJ 并查集+权值线段树合并
题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...
- bzoj 2733: [HNOI2012]永无乡 -- 线段树
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...
- YSZOJ:#247. [福利]可持久化线段树 (最适合可持久化线段树入门)
题目链接:https://syzoj.com/problem/247 解题心得: 可持久化线段树其实就是一个线段树功能的加强版,加强在哪里呢?那就是如果一颗普通的线段树多次修改之后还能知道最开始的线段 ...
- 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】【3673】可持久化并查集 & 【3674】可持久化并查集加强版
可持久化并查集 Orz hzwer & zyf 呃学习了一下可持久化并查集的姿势……其实并查集就是一个fa数组(可能还要带一个size或rank数组),那么我们对并查集可持久化其实就是实现一个 ...
- BZOJ 3674 可持久化并查集加强版(主席树变形)
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Submit: 2515 Solved: 1107 [Submit][Sta ...
- bzoj 3673&3674 可持久化并查集&加强版(可持久化线段树+启发式合并)
CCZ在2015年8月25日也就是初三暑假要结束的时候就已经能切这种题了%%% 学习了另一种启发式合并的方法,按秩合并,也就是按树的深度合并,实际上是和按树的大小一个道理,但是感觉(至少在这题上)更好 ...
随机推荐
- selenium处理div生成弹框
目前遇到的弹框有两种,一种是alert,一种是div,如果遇到div模拟的弹框,在用alert就不行了. 1. public static Alert getAlert(WebDriver dr) { ...
- 【Ext.Net学习笔记】05:Ext.Net GridPanel的用法(包含Filter、Sorter、Grouping、汇总(Summary)的用法)
GridPanel是用来显示数据的表格,与ASP.NET中的GridView类似. GridPanel用法 直接看代码: <ext:GridPanel runat="server&qu ...
- hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)
时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device w ...
- hdu-5895 Mathematician QSC(数学)
题目链接: Mathematician QSC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Jav ...
- XUtils===XUtils3框架的基本使用方法
转载自:http://blog.csdn.NET/a1002450926/article/details/50341173 今天给大家带来XUtils3的基本介绍,本文章的案例都是基于XUtils3的 ...
- 移动App崩溃测试用例设计
我们的日常生活中对移动设备越来越多的使用意味着移动App测试这个主题已成为需要考虑的一个无法避免的问题.根据最近的调查研究,用户难以容忍有bug的移动App. 移动App Bug的影响是用户体验差.A ...
- dexDebug ExecException finished with non-zero exit value 2
Error:Execution failed for task ':app:transformClassesWithDexForDebug'. com.android.build.api.transf ...
- 导航(NavanavigationController)push和pop
//跳转到指定的控制器 for (UIViewController *Vc in self.navigationController.viewControllers) { if ([Vc isKind ...
- SQLite 解决:Could not load file or assembly 'System.Data.SQLite ... 试图加载格式不正确的程序/or one of its dependencies. 找不到指定的模块。
Could not load file or assembly 'System.Data.SQLite.dll' or one of its dependencies. 找不到指定的模块. 错误提示 ...
- 限制站点目录防止跨站的三种方案(使用open_basedir)
nginx结合php的时候,可以使用open_basedir限制站点目录防止跨站具体实现方法有以下三种:注意:以下三种设置方法均需要PHP版本为5.3或者以上. 方法1)在Nginx配置文件中加入fa ...