SP16580 QTREE7 - Query on a tree VII(LCT)

时间:2023-03-09 04:50:12
SP16580 QTREE7 - Query on a tree VII(LCT)

题意翻译

一棵树,每个点初始有个点权和颜色(输入会给你)

0 u:询问所有u,v路径上的最大点权,要满足u,v路径上所有点颜色相同

1 u:反转u的颜色

2 u w:把u的点权改成w

题解

Qtree6的升级版,建议先做完再来做这题(毕竟那个代码改一改就能做这个了两倍经验岂不美哉)(Qtree6我的题解->这里

我就当你们已经都会Qtree6了(小声)

就是把Qtree6中维护连通块中点的个数,变成维护子树中的最大值就行了

实子树的max直接用,虚子树的max可以直接丢进一个set里

然后就是access的时候虚实边转化,set里要变一下(个人感觉access的时候不需要pushup,因为虚实边转化了,子树最大值是不变的)

然后就是节点0的值赋为-inf省的pushup的时候变为0,因为节点的值会有负的

然后就是update的时候只改一颗树里的竟然对了……可能是因为findroot的时候splay过了吧……

 //minamoto
#include<bits/stdc++.h>
#define Col lct[col[x]]
#define inf 0x3f3f3f3f
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,:;}
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
char sr[<<],z[];int C=-,Z;
inline void Ot(){fwrite(sr,,C+,stdout),C=-;}
inline void print(int x){
if(C><<)Ot();if(x<)sr[++C]=,x=-x;
while(z[++Z]=x%+,x/=);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=;
int f[N],Next[N<<],head[N],ver[N<<],tot,col[N],n,m,v[N];
inline void add(int u,int v){
ver[++tot]=v,Next[tot]=head[u],head[u]=tot;
ver[++tot]=u,Next[tot]=head[v],head[v]=tot;
}
struct LCT{
int fa[N],ch[N][],mx[N];
multiset<int> s[N];
LCT(){mx[]=-inf;}
inline bool isroot(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
#define lc ch[x][0]
#define rc ch[x][1]
inline void pushup(int x){
cmax(mx[x]=v[x],max(mx[lc],mx[rc]));
if(!s[x].empty()) cmax(mx[x],*s[x].rbegin());
}
void rotate(int x){
int y=fa[x],z=fa[y],d=ch[y][]==x;
if(!isroot(y)) ch[z][ch[z][]==y]=x;
fa[x]=z,fa[y]=x,fa[ch[x][d^]]=y,ch[y][d]=ch[x][d^],ch[x][d^]=y,pushup(y);
}
void splay(int x){
for(int y=fa[x],z=fa[y];!isroot(x);y=fa[x],z=fa[y]){
if(!isroot(y))
((ch[y][]==x)^(ch[z][]==y))?rotate(x):rotate(y);
rotate(x);
}
pushup(x);
}
void access(int x){
for(int y=;x;x=fa[y=x]){
splay(x);
if(rc) s[x].insert(mx[rc]);
if(rc=y) s[x].erase(s[x].find(mx[y]));
}
}
int findroot(int x){
access(x),splay(x);
while(lc) x=lc;
splay(x);
return x;
}
void link(int x){
access(x),splay(x);
int y=fa[x]=f[x];
access(y),splay(y);
ch[y][]=x,pushup(y);
}
void cut(int x){
access(x),splay(x);
lc=fa[lc]=;
pushup(x);
}
void update(int x){
access(x),splay(x);
v[x]=read(),pushup(x);
}
}lct[];
void dfs(int x){
for(int i=head[x];i;i=Next[i]){
int v=ver[i];
if(v==f[x]) continue;
f[v]=x,dfs(v);
}
Col.link(x);
}
int main(){
//freopen("testdata.in","r",stdin);
n=read();
for(int i=;i<n;++i){
int u=read(),v=read();
add(u,v);
}
for(int i=;i<=n;++i) col[i]=read();
for(int i=;i<=n;++i) v[i]=read();
f[]=n+,dfs();
m=read();
while(m--){
int op=read(),x=read();
switch(op){
case :print(Col.mx[Col.ch[Col.findroot(x)][]]);break;
case :Col.cut(x),col[x]^=,Col.link(x);break;
case :Col.update(x);break;
}
}
Ot();
return ;
}