AAA树学习笔记
AAA 树是一种基于 LCT 的数据结构,属于类Top Tree的一种,是最通用化的树分治数据结构。为了维护子树信息,在每个结点上额外加一棵辅助树维护其所有的出边。
AAA 树有两种结点:compress 与 rake。每个 compress 结点表示原树中的一个结点,每个 rake 结点表示原树中的一条有向出边(即,每条边会由两个 rake 结点维护)。
所有的不可拆分的信息本身都放在 compress 结点上。
每个 compress 结点都有三个指针,分别为l、r与 c。l 与 r 指向了其在 compress 树 中的孩子,c 指向了维护其所有出边的 rake 树。
每个 rake 结点也有三个指针,分别为 l、r 与 c。l 与 r 指向了其在 rake 树中的孩子,c 指向了维护以该出边为根的实链的 compress 树,特殊的,如果这条出边指向了该结点的父亲或是实孩子,则 c 为空。
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x=0; int f=1; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-')f=-1; ch=getchar();}
while(isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
struct Tag
{
int mul,add;
Tag(int mulv=1,int addv=0)
{mul=mulv,add=addv;}
bool emp() {return mul==1 && add==0;}
friend Tag operator + (Tag u,Tag v)
{return Tag(u.mul*v.mul,u.add*v.mul+v.add);}
};
struct Data
{
int mx,mn,sm,sz;
Data(int mxv=0,int mnv=0,int smv=0,int szv=0)
{mx=mxv,mn=mnv,sm=smv,sz=szv;}
friend Data operator + (Data u,Tag v)
{
if(u.sz)
{
int mxv=u.mx*v.mul+v.add;
int mnv=u.mn*v.mul+v.add;
int smv=u.sm*v.mul+v.add*u.sz;
return Data(mxv,mnv,smv,u.sz);
}
return u;
}
friend Data operator + (Data u,Data v)
{
int mxv=max(u.mx,v.mx);
int mnv=min(u.mn,v.mn);
int smv=u.sm+v.sm;
int szv=u.sz+v.sz;
return Data(mxv,mnv,smv,szv);
}
};
const int MAXN = 200005;
struct Edge { int from,to; } e[MAXN];
int fa[MAXN],ch[MAXN][4],val[MAXN];
Tag stag[MAXN],xtag[MAXN];
Data sld[MAXN],vir[MAXN],all[MAXN];
bool rev[MAXN],isx[MAXN];
int bin[MAXN],cnt,ft;
int rt,n,m;
inline void pushr(int x)
{swap(ch[x][0],ch[x][1]),rev[x]^=1;}
inline void tagsld(int x,Tag w)
{
stag[x]=stag[x]+w; sld[x]=sld[x]+w;
val[x]=val[x]*w.mul+w.add;
all[x]=sld[x]+vir[x];
}
inline void tagvir(int x,Tag w,bool fg=1)
{
xtag[x]=xtag[x]+w; all[x]=all[x]+w;
vir[x]=vir[x]+w;
if(fg) tagsld(x,w);
}
inline void pushup(int x)
{
sld[x]=vir[x]=all[x]=Data(-(1<<30),1<<30,0,0);
if(!isx[x]) all[x]=sld[x]=Data(val[x],val[x],val[x],1);
for (int i = 0; i < 2; ++i)
{
if(ch[x][i])
{
sld[x]=sld[x]+sld[ch[x][i]];
vir[x]=vir[x]+vir[ch[x][i]];
}
}
for (int i = 0; i < 4; ++i)
if(ch[x][i]) all[x]=all[x]+all[ch[x][i]];
for (int i = 2; i < 4; ++i)
if(ch[x][i]) vir[x]=vir[x]+all[ch[x][i]];
}
inline void pushdown(int x)
{
if(rev[x])
{
if(ch[x][0]) pushr(ch[x][0]);
if(ch[x][1]) pushr(ch[x][1]);
rev[x]=0;
}
if(!xtag[x].emp())
{
for (int i = 0; i < 4; ++i)
if(ch[x][i]) tagvir(ch[x][i],xtag[x],i>=2);
xtag[x]=Tag(1,0);
}
if(!stag[x].emp())
{
for (int i = 0; i < 2; ++i)
if(ch[x][i]) tagsld(ch[x][i],stag[x]);
stag[x]=Tag(1,0);
}
}
inline int son(int x,int y)
{
if(ch[x][y]) pushdown(ch[x][y]);
return ch[x][y];
}
inline int find(int x)
{
for (int i = 0; i < 4; ++i)
if(ch[fa[x]][i]==x) return i;
return -1;
}
inline void sets(int x,int w,int tp)
{if(w) fa[w]=x; ch[x][tp]=w;}
inline bool chk(int x,int tp=0)
{
if(!tp)
return !fa[x] || ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;
return !fa[x] || !isx[x] || !isx[fa[x]];
}
inline void rotate(int x,int tp)
{
if(chk(x,tp)) return;
int y=fa[x];
if(fa[y]) sets(fa[y],x,find(y));
else fa[x]=0; fa[y]=x;
if(ch[y][tp]==x)
{
ch[y][tp]=ch[x][tp+1];
ch[x][tp+1]=y;
if(ch[y][tp])
fa[ch[y][tp]]=y;
}
else
{
ch[y][tp+1]=ch[x][tp];
ch[x][tp]=y;
if(ch[y][tp+1])
fa[ch[y][tp+1]]=y;
}
pushup(y); pushup(x);
}
inline void update(int x)
{if(fa[x]) update(fa[x]); pushdown(x);}
inline void split(int x,int tp=0)
{
pushup(x);
while(!chk(x,tp))
{
int y=fa[x];
if(chk(y,tp)) rotate(x,tp);
else
{
if((ch[fa[y]][tp]==y)^(ch[y][tp]==x))
rotate(x,tp);
else rotate(y,tp);
rotate(x,tp);
}
}
}
inline int newnode()
{
int x=ft ? bin[ft--] : ++cnt;
for (int i = 0; i < 4; ++i)
ch[x][i]=0;
fa[x]=0; stag[x]=xtag[x]=Tag(1,0);
sld[x]=vir[x]=all[x]=Data(-(1<<30),1<<30,0,0);
isx[x]=1; rev[x]=val[x]=0;
return x;
}
inline void recycle(int x)
{ bin[++ft]=x; }
inline void add(int x,int w)
{
for (int i = 2; i < 4; ++i)
if(!ch[w][i]) { sets(w,x,i); return;}
int y=newnode(),u;
for (u = w; isx[ch[u][2]]; u = son(u,2));
sets(y,ch[u][2],2); sets(y,x,3);
sets(u,y,2); split(y,2);
}
inline void del(int x)
{
if(isx[fa[x]])
{
sets(fa[fa[x]],ch[fa[x]][5-find(x)],find(fa[x]));
recycle(fa[x]); split(fa[fa[x]],2);
}
else sets(fa[x],0,find(x));
fa[x]=0;
}
inline void access(int x)
{
int y; update(x); split(x);
if(ch[x][1])
{
y=ch[x][1]; ch[x][1]=0;
add(y,x); pushup(x);
}
while(fa[x])
{
for(y = fa[x]; isx[y]; y = fa[y]);
split(y);
if(ch[y][1])
{
sets(fa[x],ch[y][1],find(x));
split(fa[x],2);
}
else del(x); sets(y,x,1);
pushup(y); x=y;
}
}
inline void makeroot(int x)
{access(x),split(x),pushr(x);}
inline int findroot(int x)
{
access(x); split(x); x=son(x,0);
while(x && ch[x][1])
x=son(x,1);
return x;
}
inline int getroot(int x)
{
while(fa[x]) x=fa[x];
return x;
}
inline int cut(int x)
{
int y=findroot(x);
if(y)
{
access(y); split(y);
del(x); pushup(y);
}
return y;
}
inline void link(int x,int w)
{
int y=cut(x);
if(getroot(x)!=getroot(w))
y=w;
if(y)
{
access(y); split(y);
add(x,y); pushup(y);
}
}
int main(int argc, char const *argv[])
{
n=read(); m=read(); rt=1;
for (int i = 1; i < n; ++i)
e[i].from=read(),e[i].to=read();
for (int i = 1; i <= n; ++i)
val[++cnt]=read(),pushup(cnt);
for (int i = 1; i < n; ++i)
{
makeroot(e[i].from);
makeroot(e[i].to);
link(e[i].from,e[i].to);
}
rt=read(); makeroot(rt);
int opt,u,w;
for (int i = 1; i <= m; ++i)
{
opt=read(); u=read();
if(opt==1) makeroot(u),rt=u;
else if(opt==9) link(u,read());
else if(opt==0 || opt==3 || opt==4 || opt==5 || opt==11)
{
access(u); split(u);
if(opt==3 || opt==4 || opt==11)
{
int ans=val[u];
for (int j = 2; j < 4; ++j)
{
if(ch[u][j])
{
Data p=all[ch[u][j]];
if(opt==3) ans=min(ans,p.mn);
if(opt==4) ans=max(ans,p.mx);
if(opt==11) ans+=p.sm;
}
}
printf("%d\n", ans);
}
else
{
Tag p=Tag(opt==5,read());
val[u]=val[u]*p.mul+p.add;
for (int j = 2; j < 4; ++j)
if(ch[u][j]) tagvir(ch[u][j],p);
pushup(u);
}
}
else
{
makeroot(u); access(read()); split(u);
if(opt==7 || opt==8 || opt==10)
{
Data ans=sld[u];
if(opt==7) printf("%d\n", ans.mn);
if(opt==8) printf("%d\n", ans.mx);
if(opt==10) printf("%d\n", ans.sm);
}
else tagsld(u,Tag(opt==6,read()));
makeroot(rt);
}
}
return 0;
}