SP16580 QTREE7 - Query on a tree VII(LCT 维护树上同色点连通块 + LCT维护子树最大点权 + multiset)

在这里插入图片描述


建立两棵 LCT,分别维护颜色为 0 的图和颜色为 1 的图,维护的方法同 SP16549 QTREE6 - Query on a tree VI。

对 LCT 上的每个节点,要维护子树点权最大值,虚子树的信息用 multiset 维护,对于操作3要修改两棵 LCT 的点权,而不是一棵。

如果 pushup 采用两段直接合并,注意0号点的 v a l val val m x v a l mxval mxval 要赋值为负无穷小,因为节点权值可以为负数。


代码:

#include<bits/stdc++.h>
using namespace std;
#define R register int
#define I inline void
const int maxn = 1e6 + 10;
typedef long long ll;
int n,m,cnt;
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 Graph {
	int head[maxn],nxt[maxn << 1],to[maxn << 1],cnt;
	void init() {
		memset(head,-1,sizeof head);
		cnt = 0;
	}
	void add(int u,int v) {
		to[cnt] = v;
		nxt[cnt] = head[u];
		head[u] = cnt++;
		to[cnt] = u;
		nxt[cnt] = head[v];
		head[v] = cnt++;
	}
}G;
int col[maxn],p[maxn],a[maxn];
struct LCT {						//用splay维护原森林的连通,用到了splay的操作以及数组 
	#define ls ch[x][0]
	#define rs ch[x][1]
	#define inf 0x3f3f3f3f
	int ch[maxn][2];				//ch[u][0] 表示 左二子,ch[u][1] 表示右儿子
	int f[maxn];					//当前节点的父节点 
	int tag[maxn];					//翻转标记,乘标记,加标记 
	int top,sta[maxn],val[maxn],mxval[maxn];
	multiset<int> son[maxn];
	void init() {
		val[0] = mxval[0] = -0x7f7f7f7f;
		for (int i = 1; i <= n; i++)
			val[i] = mxval[i] = a[i];
	}
	inline bool get(int x) {
    	return ch[f[x]][1] == x;
	}
	inline void pushup(int x) {
		mxval[x] = max(mxval[ls],max(val[x],mxval[rs]));
		if (son[x].size())
			mxval[x] = max(mxval[x],*son[x].rbegin());
	}
	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 的根
    	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);
			if (rs) son[x].insert(mxval[rs]);
			if (lst) {
				auto t = son[x].find(mxval[lst]);
				son[x].erase(t);
			}
			rs = lst;
			pushup(x);
			lst = x; x = f[x];
		}
	}
	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) {					//连接 x 和 原树上父节点 y 的边 
		splay(x); int y = p[x];
		access(y); splay(y);
		f[x] = y; son[y].insert(mxval[x]);					//连实边 
		pushup(y);
	}
	inline void cut(int x) {						//cut 掉原树上 x 和 父节点的边 
		access(x); splay(x);
		f[ch[x][0]] = 0;
		ch[x][0] = 0;
		pushup(x);
	}
}tree[2];
int op,id,x,y,u,v,w;
void dfs(int u,int fa) {
	p[u] = fa;
	for (int i = G.head[u]; i + 1; i = G.nxt[i]) {
		int v = G.to[i];
		if (v == fa) continue;
		dfs(v,u);
	}
	tree[col[u]].link(u);
}
int main() {
	n = read(); G.init();
	for (int i = 1; i < n; i++) {
		u = read(); v = read();
		G.add(u,v);
	} 
	for (int i = 1; i <= n; i++)
		col[i] = read();	
	for (int i = 1; i <= n; i++)
		a[i] = read();
	tree[0].init(); tree[1].init();	
	dfs(1,n + 1);
	m = read();
	while (m--) {
		op = read(); u = read();
		if (op == 0) {
			int c = col[u];
			int t = tree[c].findroot(u);
			tree[c].splay(t);
			printf("%d\n",tree[c].mxval[tree[c].ch[t][1]]);
		} else if (op == 1) {
			tree[col[u]].cut(u);
			tree[col[u] ^= 1].link(u);
		} else {
			w = read();
			a[u] = w;
			tree[0].access(u);
			tree[0].splay(u);
			tree[0].val[u] = w;
			tree[0].pushup(u);
			
			tree[1].access(u);
			tree[1].splay(u);
			tree[1].val[u] = w;
			tree[1].pushup(u);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值