这题树形没有改变,实际上用不着LCT,用树剖就行。
考虑树剖轻重链剖分的做法:将树按轻重链剖分后,建线段树,线段树的每个节点维护对应区间的左端点颜色,右端点颜色,以及答案。
两个区间合并时,更新区间的左端点颜色和右端点颜色,大区间的答案等于两个子区间的答案相加,若两个区间的交接处颜色相同,则答案需要减一。
查询时,由于会在树上跳 log 次,每一次查询的结果要和上一次查询的结果进行合并。
LCT 同样是用 splay 维护树链信息,(不过这个树链不是轻重链)在 LCT 上提取一条路径要比树剖更加容易,且 splay 是按深度为关键字维护这条链,在 splay 上维护 u u u 到子树节点区间的答案和区间左端点颜色、区间右端点颜色。
子树区间合并维护父节点方法同树剖,查询时将路径提出来直接查询根节点,修改时同样只需要在根节点打个标记,注意给LCT换根时的区间翻转操作还要翻转这个节点的左端点颜色和右端点颜色。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
typedef long long ll;
int n,m;
const int mod = 51061;
inline int read(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}
struct LCT { //用splay维护原森林的连通,用到了splay的操作以及数组
int ch[maxn][2]; //ch[u][0] 表示 左二子,ch[u][1] 表示右儿子
int f[maxn]; //当前节点的父节点
int tag[maxn]; //翻转标记,乘标记,加标记
int top,sta[maxn],sz[maxn];
int col[maxn],lcol[maxn],rcol[maxn],ans[maxn];
int laz[maxn];
inline bool get(int x) {
return ch[f[x]][1] == x;
}
void init() {
memset(f,0,sizeof f);
memset(ch,0,sizeof ch);
memset(tag,0,sizeof tag);
memset(laz,0,sizeof laz);
}
inline void pushup(int rt) {
if (rt) {
ans[rt] = sz[rt] = 1;
lcol[rt] = rcol[rt] = col[rt];
int ls = ch[rt][0], rs = ch[rt][1];
if (ls) {
sz[rt] += sz[ls];
ans[rt] += ans[ls];
if (lcol[rt] == rcol[ls]) ans[rt]--;
lcol[rt] = lcol[ls];
}
if (rs) {
sz[rt] += sz[rs];
ans[rt] += ans[rs];
if (rcol[rt] == lcol[rs]) ans[rt]--;
rcol[rt] = rcol[rs];
}
}
}
inline void pushdown(int rt) {
if (tag[rt]) {
int ls = ch[rt][0], rs = ch[rt][1];
if (ls) swap(ch[ls][0],ch[ls][1]), swap(lcol[ls],rcol[ls]), tag[ls] ^= 1;
if (rs) swap(ch[rs][0],ch[rs][1]), swap(lcol[rs],rcol[rs]), tag[rs] ^= 1;
tag[rt] = 0;
}
if (laz[rt]) {
int ls = ch[rt][0], rs = ch[rt][1];
if (ls) {
laz[ls] = lcol[ls] = rcol[ls] = col[ls] = laz[rt];
ans[ls] = 1;
}
if (rs) {
laz[rs] = lcol[rs] = rcol[rs] = col[rs] = laz[rt];
ans[rs] = 1;
}
laz[rt] = 0;
}
}
inline bool isroot(int x) {
return (ch[f[x]][0] != x) && (ch[f[x]][1] != x);
}
inline void rotate(int x) { //旋转操作,根据 x 在 f[x] 的哪一侧进行左旋和右旋
int old = f[x], oldf = f[old];
int whichx = get(x);
if(!isroot(old)) ch[oldf][ch[oldf][1] == old] = x; //如果 old 不是根节点,就要修改 oldf 的子节点信息
ch[old][whichx] = ch[x][whichx ^ 1];
ch[x][whichx ^ 1] = old;
f[ch[old][whichx]] = old;
f[old] = x; f[x] = oldf;
pushup(old); pushup(x);
}
inline void splay(int x) { //将 x 旋到所在 splay 的根
top = 0; sta[++top] = x;
for (int i = x; !isroot(i); i = f[i]) sta[++top] = f[i]; //在 splay 中维护 下推标记
while(top) pushdown(sta[top--]);
for(int fa = f[x]; !isroot(x); rotate(x), fa = f[x]) { //再把x翻上来
if(!isroot(fa)) //如果fa非根,且x 和 fa是同一侧,那么先翻转fa,否则先翻转x
rotate((get(x) == get(fa)) ? fa : x);
}
}
inline void access(int x) { //access操作将x 到 根路径上的边修改为重边
int lst = 0;
while(x > 0) {
splay(x);
ch[x][1] = lst;
pushup(x);
lst = x; x = f[x];
}
}
inline void move_to_root(int x) { //将 x 移到 x 所在树的根(不是所在splay的根,所在splay只是一条重链)
access(x); splay(x); tag[x] ^= 1; swap(ch[x][0],ch[x][1]); swap(lcol[x],rcol[x]);
//将 x 移到 根之后 x 是深度最低的点,这条重链、这棵splay上所有点的深度颠倒,
//所有的点的左子树的点应该到右子树,因此要翻转这棵splay的左右子树
}
inline int findroot(int x) {
access(x);
splay(x);
int rt = x;
while(ch[rt][0]) rt = ch[rt][0];
return rt;
}
inline void link(int x,int y) {
move_to_root(x); f[x] = y; splay(x);
}
inline void cut(int x,int y) {
move_to_root(x); access(y);
splay(y); ch[y][0] = f[x] = 0;
pushup(y);
}
}tree;
int x,y,u,v,k[maxn];
char op[10];
int main() {
n = read(); m = read();
tree.init();
for (int i = 1; i <= n; i++) {
tree.col[i] = tree.lcol[i] = tree.rcol[i] = read();
tree.ans[i] = 1;
}
for (int i = 1; i < n; i++) {
int u,v; u = read(), v = read();
tree.link(u,v);
}
while (m--) {
scanf("%s",op);
if (op[0] == 'Q') {
u = read(); v = read();
tree.move_to_root(u);
tree.access(v);
tree.splay(v);
printf("%d\n",tree.ans[v]);
} else {
u = read(); v = read(); x = read();
tree.move_to_root(u);
tree.access(v);
tree.splay(v);
tree.col[v] = tree.lcol[v] = tree.rcol[v] = tree.laz[v] = x;
tree.ans[v] = 1;
}
}
return 0;
}