背景:
两道
模板题,放一起写了。
不水积分了。
题目传送门:
https://www.luogu.org/problemnew/show/P3690
https://www.luogu.org/problemnew/show/P2147
思路:
好像比
更模板,什么都不用维护,只是
中联通的问题。
不知道为什么在
中最后加上
就会
,欢迎大佬指正。
网上的学习笔记有很多(注意:有些大佬的
有些小
,多看几篇),在这里就不写了。
懒…
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{int d,fa,sum,lazy,son[2];} tr[300010];
int n,m;
bool isroot(int x)
{
return tr[tr[x].fa].son[0]!=x&&tr[tr[x].fa].son[1]!=x;
}
bool isson(int x)
{
return x==tr[tr[x].fa].son[1];
}
void change(int x)
{
if(!x)return;
swap(tr[x].son[0],tr[x].son[1]);
tr[x].lazy^=1;
}
void pushup(int x)
{
tr[x].sum=tr[x].d^tr[tr[x].son[0]].sum^tr[tr[x].son[1]].sum;
}
void pushdown(int x)
{
if(!tr[x].lazy) return;
change(tr[x].son[0]),change(tr[x].son[1]);
tr[x].lazy=0;
}
void rot(int x)
{
int w=isson(x),y=tr[x].fa,yy=tr[y].fa;
tr[y].son[w]=tr[x].son[w^1];
if(tr[y].son[w]) tr[tr[y].son[w]].fa=y;
tr[x].fa=yy;
if(!isroot(y)) tr[yy].son[isson(y)]=x;
tr[x].son[w^1]=y;tr[y].fa=x;
pushup(y);
}
int sta[300010];
int top;
void splay(int x)
{
sta[top=1]=x;
for(int i=x;!isroot(i);i=tr[i].fa)
sta[++top]=tr[i].fa;
while(top) pushdown(sta[top--]);
for(int y=tr[x].fa;!isroot(x);rot(x),y=tr[x].fa)
if(!isroot(y)) isson(x)^isson(y)?rot(x):rot(y);
pushup(x);
}
void access(int x)
{
for(int y=0;x;y=x,x=tr[x].fa)
splay(x),tr[x].son[1]=y,pushup(x);
}
int findroot(int x)
{
access(x);splay(x);
while(tr[x].son[0]) x=tr[x].son[0];
//splay(x);
return x;
}
void makeroot(int x)
{
access(x);splay(x);change(x);
}
void link(int x,int y)
{
makeroot(x);
if(findroot(y)!=x) tr[x].fa=y;
}
void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
void del(int x,int y)
{
makeroot(x);
if(findroot(y)==x&&!tr[x].son[1]&&tr[x].fa==y) tr[x].fa=tr[y].son[0]=0,pushup(x),pushup(y);
}
int main()
{
int t,x,y;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
tr[i]=(node){0,0,0,0,{0,0}};
scanf("%d",&tr[i].d);
pushup(i);
}
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&t,&x,&y);
switch(t)
{
case 0:split(x,y);printf("%d\n",tr[y].sum);break;
case 1:link(x,y);break;
case 2:del(x,y);break;
case 3:splay(x);tr[x].d=y;pushup(x);break;
}
}
}
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{int fa,lazy,son[2];} tr[300010];
int n,m;
bool isroot(int x)
{
return tr[tr[x].fa].son[0]!=x&&tr[tr[x].fa].son[1]!=x;
}
bool isson(int x)
{
return x==tr[tr[x].fa].son[1];
}
void change(int x)
{
if(!x)return;
swap(tr[x].son[0],tr[x].son[1]);
tr[x].lazy^=1;
}
void pushdown(int x)
{
if(!tr[x].lazy) return;
change(tr[x].son[0]),change(tr[x].son[1]);
tr[x].lazy=0;
}
void rot(int x)
{
int w=isson(x),y=tr[x].fa,yy=tr[y].fa;
tr[y].son[w]=tr[x].son[w^1];
if(tr[y].son[w]) tr[tr[y].son[w]].fa=y;
tr[x].fa=yy;
if(!isroot(y)) tr[yy].son[isson(y)]=x;
tr[x].son[w^1]=y;tr[y].fa=x;
}
int sta[300010];
int top;
void splay(int x)
{
sta[top=1]=x;
for(int i=x;!isroot(i);i=tr[i].fa)
sta[++top]=tr[i].fa;
while(top) pushdown(sta[top--]);
for(int y=tr[x].fa;!isroot(x);rot(x),y=tr[x].fa)
if(!isroot(y)) isson(x)^isson(y)?rot(x):rot(y);
}
void access(int x)
{
for(int y=0;x;y=x,x=tr[x].fa)
splay(x),tr[x].son[1]=y;
}
int findroot(int x)
{
access(x);splay(x);
while(tr[x].son[0]) x=tr[x].son[0];
//splay(x);
return x;
}
void makeroot(int x)
{
access(x);splay(x);change(x);
}
void link(int x,int y)
{
makeroot(x);
if(findroot(y)!=x) tr[x].fa=y;
}
void del(int x,int y)
{
makeroot(x);
if(findroot(y)==x&&!tr[x].son[1]&&tr[x].fa==y) tr[x].fa=tr[y].son[0]=0;
}
int main()
{
char s[10];
int x,y;
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%s %d %d",s+1,&x,&y);
switch(s[1])
{
case 'Q':printf(findroot(x)==findroot(y)?"Yes\n":"No\n");break;
case 'C':link(x,y);break;
case 'D':del(x,y);break;
}
}
}