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

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

标签(空格分隔): OI-BZOJ OI-可持久化数组 OI-并查集


Time Limit: 15 Sec
Memory Limit: 256 MB


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


Solution####

强制在线,数据加强,方法和上题一样
BZOJ 3673: 可持久化并查集 by zky


Code####

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<vector>
using namespace std;
int read()
{
 	int s=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
	return s*f;
}
//smile please
struct tree;
extern tree t[12000005];
struct tree
{
	int w[2];
}t[12000005];
int n,m,c,np,timee,la,lans;
int st[200005];
int ask(int x,int s,int c)
{
	if(c==-1)return t[x].w[0];
	int p=(s>>c)&1;
	return ask(t[x].w[p],s-(p<<c),c-1);
}
void modif(int x,int s,int c,int num)
{
	int now=++np;
	t[now]=t[x];
	if(c==-1){t[now].w[0]=num;return;}
	int p=(s>>c)&1;
	modif(t[now].w[p],s-(p<<c),c-1,num);
	t[now].w[p]=now+1;
}
int gf(int x)
{
	int fx=ask(st[timee],x,c-1);
	if(fx==x)
	   return x;
	else
	  {fx=gf(fx);
	   la=np+1;
	   modif(st[timee],x,c-1,fx);
	   st[timee]=la;
	   return fx;
	  }
}
int main()
{
	n=read(),m=read();
	c=ceil(log(n)/log(2));
	for(int i=1;i<=n;i++)
	    la=np+1,
		modif(st[0],i,c-1,i),
		st[0]=la;
	for(timee=1;timee<=m;timee++)
	   {int o=read();
		if(o==2)
		   st[timee]=st[read()^lans];
		else
	       st[timee]=st[timee-1];
	    if(o==1)
	      {int a=read()^lans,b=read()^lans;
	       gf(a),gf(b);
	       int fa=ask(st[timee],a,c-1),
	           fb=ask(st[timee],b,c-1);
		   if(fa!=fb)
		      la=np+1,
		      modif(st[timee],fa,c-1,fb),
		      st[timee]=la;
		  }
	   	if(o==3)
	      {int a=read()^lans,b=read()^lans;
	       gf(a),gf(b);
	       int fa=ask(st[timee],a,c-1),
	           fb=ask(st[timee],b,c-1);
	       lans=(fa==fb);
		   printf("%d\n",fa==fb);
		  }
	   }
	return 0;
}

posted on 2016-03-04 11:12  wuyuhan  阅读(148)  评论(0编辑  收藏  举报

导航