社区讨论
lct 14pts求调
P3690【模板】动态树(LCT)参与者 3已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @m1glryho
- 此快照首次捕获于
- 2024/09/24 23:40 去年
- 此快照最后确认于
- 2025/11/04 18:50 4 个月前
仅过 #10 #12。
CPP#include <bits/stdc++.h>
//#include <windows.h>
#define ED cerr<<endl;
#define TS cerr<<"I AK IOI"<<endl;
#define cr(x) cerr<<x<<endl;
#define cr2(x,y) cerr<<x<<" "<<y<<endl;
#define cr3(x,y,z) cerr<<x<<" "<<y<<" "<<z<<endl;
#define cr4(x,y,z,w) cerr<<x<<" "<<y<<" "<<z<<" "<<w<<endl;
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
#define ls(x) tr[x].s[0]
#define rs(x) tr[x].s[1]
#define fa(x) tr[x].fa
#define get(x) (rs(fa(x))==x)
#define isroot(x) (ls(fa(x))!=x&&rs(fa(x))!=x)
using namespace std;
const int N=1e5+5,INF=2e9,mod=1e9+7;
int n,m;
int a[N];
struct tree {
int s[2],fa,val,tag;
}tr[N];
int stk[N];
void pushup(int u) {
tr[u].val=tr[ls(u)].val^tr[rs(u)].val^a[u];
}
void add(int u) {
swap(ls(u),rs(u)),tr[u].tag^=1;
}
void pushdown(int u) {
if(tr[u].tag!=0) {
if(ls(u)) add(ls(u));
if(rs(u)) add(rs(u));
tr[u].tag=0;
}
}
void rotate(int x) {
int y=fa(x),z=fa(y),k=get(x),w=tr[x].s[k^1];
if(!isroot(y)) tr[z].s[rs(z)==y]=x;fa(x)=z;
tr[y].s[k]=w,fa(w)=y;
tr[x].s[k^1]=y,fa(y)=x;
pushup(y),pushup(x);
}
void update(int x) {
int top=0;stk[++top]=x;
if(!isroot(x)) stk[++top]=x=fa(x);
while(top) pushdown(stk[top--]);
}
void Splay(int x) {
update(x);
while(!isroot(x)) {
if(!isroot(fa(x))) {
rotate(get(fa(x))==get(x)?fa(x):x);
}
rotate(x);
}
}
int Access(int x) {
int p;
for(p=0;x;p=x,x=fa(x)) {
Splay(x),rs(x)=p,pushup(x);
}
return p;
}
void makeroot(int x) {
x=Access(x),add(x);
}
int Find(int x) {
Access(x),Splay(x),pushdown(x);
while(ls(x)) x=ls(x),pushdown(x);
return x;
}
void link(int x,int y) {
makeroot(x);
if(Find(y)!=x) fa(x)=y;
}
void spilt(int x,int y) {
makeroot(x),Access(y),Splay(y);
}
void cut(int x,int y) {
makeroot(x);
if(Find(y)==x&&fa(x)==y&&!rs(x)) {
fa(x)=ls(y)=0,pushup(y);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) {
scanf("%d",&a[i]);
}
int op,x,y;
while(m--) {
scanf("%d%d%d",&op,&x,&y);
if(op==0) spilt(x,y),printf("%d\n",tr[y].val);
else if(op==1) link(x,y);
else if(op==2) cut(x,y);
else if(op==3) Splay(x),a[x]=y,pushup(x);
}
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...