这是一个句子测试

12
4
2015
0

BZOJ3673/3674可持久化并查集

【题目描述】

Description

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

n个集合 m个操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之后的状态(查询算作操作)
3 a b 询问a,b是否属于同一集合,是则输出1否则输出0
请注意本题采用强制在线,所给的a,b,k均经过加密,加密方法为x = x xor lastans,lastans的初始值为0
0<n,m<=2*10^5


 

Input

 

Output

 

Sample Input

5 6
1 1 2
3 1 2
2 1
3 0 3
2 1
3 1 2

Sample Output

1
0
1

HINT

 

Source

【题解】

其实窝萌可以不用路径压缩,只要按秩合并就可以了。

那么我们需要一个可持久化数组来维护每个集合的大小和关键字。

那么用可持久化线段树来实现就可以了。

然而在交加强版的时候无故RE不多说。。

【Codes】

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=210000;
struct Node{int l,r,fa,cnt;}tree[N*50];
int root[N],cnt,n,m;
void update(int l,int r,int &p,int x,int y,int z){
	tree[++cnt]=tree[p];p=cnt;
	if(l==r){tree[p].fa=y;tree[p].cnt=z;return;}
	int mid=l+r>>1;
	if(x<=mid)update(l,mid,tree[p].l,x,y,z);else update(mid+1,r,tree[p].r,x,y,z);
}
int query(int l,int r,int p,int x){
	if(l==r)return p;
	int mid=l+r>>1;
	if(x<=mid)return query(l,mid,tree[p].l,x);else return query(mid+1,r,tree[p].r,x);
}
int search(int p,int x){
	int t=query(1,n,p,x);
	if(tree[t].fa==x)return t;else return search(p,tree[t].fa);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)update(1,n,root[0],i,i,1);
	int ans=0;
	for(int i=1;i<=m;i++){
		int op,x,y;
		scanf("%d",&op);
		root[i]=root[i-1];
		if(op==1){
			scanf("%d%d",&x,&y);
			x^=ans;
			y^=ans;
			int p=search(root[i],x),q=search(root[i],y);
			if(tree[p].fa==tree[q].fa)continue;
			if(tree[p].cnt>tree[q].cnt)swap(p,q);
			update(1,n,root[i],tree[p].fa,tree[q].fa,0);
			update(1,n,root[i],tree[q].fa,tree[q].fa,tree[p].cnt+tree[q].cnt);
		}else
		if(op==2){
			scanf("%d",&x);
			x^=ans;
			root[i]=root[x];
		}else{
			scanf("%d%d",&x,&y);
			x^=ans;
			y^=ans;
			int p=search(root[i],x),q=search(root[i],y);
			printf("%d\n",ans=(tree[p].fa==tree[q].fa));
		}
	}
}
Category: 蒟蒻的BZOJ刷题记 | Tags: | Read Count: 169

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com