bzoj3674 可持久化並查集加強版
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
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
主席樹+按秩合併(似乎路徑壓縮並沒有什麼用QwQ)
還有 不寫xor lastans竟然也能對…
#include <bits/stdc++.h>
#define N 200010
#define M 8000010
#define mid ((l+r)>>1)
using namespace std;
int n,m;
struct tree{
tree *ch[2]; int d,dep;
tree(){
d=0; dep=1;
}
}t[M],*root[N],*tail=t;
template <class Aqua>
inline void read(Aqua &s){
s=0; char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) s=s*10+c-'0',c=getchar();
}
tree *newnode(){
(tail+1)->ch[0]=(tail+1)->ch[1]=t;
return ++tail;
}
void modify(tree *&cur,tree *pre,int l,int r,int pos,int d,int dep){
cur=newnode();
if (l==r){
cur->d=d; cur->dep=dep;
return;
}
cur->ch[0]=pre->ch[0]; cur->ch[1]=pre->ch[1];
if (pos<=mid)
modify(cur->ch[0],pre->ch[0],l,mid,pos,d,dep);
else
modify(cur->ch[1],pre->ch[1],mid+1,r,pos,d,dep);
}
tree *query(tree *cur,int l,int r,int pos){
if (l==r) return cur;
if (pos<=mid)
return query(cur->ch[0],l,mid,pos);
else
return query(cur->ch[1],mid+1,r,pos);
}
tree *Query(tree *cur,int x){
for (tree *f;;x=f->d){
f=query(cur,1,n,x);
if (f->d==x)
return f;
}
}
void pre(){
t->ch[0]=t->ch[1]=root[0]=t;
for (int i=1;i<=n;i++)
modify(root[0],root[0],1,n,i,i,1);
}
int main(){
read(n),read(m);
pre();
int op,a,b,las(0); tree *x,*y;
for (int i=1;i<=m;i++){
read(op); root[i]=root[i-1];
if (op==1){
read(a),read(b);
a^=las,b^=las;
x=Query(root[i],a),y=Query(root[i],b);
if (x->d==y->d)
continue;
if (x->dep>y->dep)
swap(x,y);
modify(root[i],root[i],1,n,x->d,y->d,y->dep);
if (x->dep==y->dep)
modify(root[i],root[i],1,n,y->d,y->d,y->dep+1);
}
if (op==2){
read(a); a^=las;
root[i]=root[a];
}
if (op==3){
read(a),read(b);
a^=las,b^=las;
x=Query(root[i],a),y=Query(root[i],b);
las=(x->d==y->d);
printf("%d\n",las);
}
}
return 0;
}