和 SP2939 QTREE5 - Query on a tree V 类似的做法,考虑 LCT 每个节点维护 ,表示这条链的深度最低点的答案和深度最高点的答案。
由于整个过程存在连边和删边,肯定要用到 操作,但这题换根时只要交换一下 即可,因为 和贡献有关,与位置无关,换根不会改变它们的值。
对于 pushup,由于这题有点卡常,原来的三段合并常数太大(指令更多,而pushup操作执行非常频繁),改写成两段合并。由于这题有边权,拆边为点,边权赋为点权,左右子树合并时需要计算右子树的黑点对 的额外贡献( 同理),这段额外贡献就是右子树的黑点数量 ✖ 黑点到左子树左端点的距离,如何得到这段距离?这段距离其实就是左子树的点权和,画条链拆一下就清楚了。
考虑如图一条链,
为边拆开得到的点,左子树的链为
,右子树的链为
,要合并左右子树(
和
) 得到
以
为分界线 ,合并时可能的情况有两种:
右子树需要计算的额外贡献是 右子树黑点个数乘上
(
为点权,这里代表边权)。
合并时可能出现:
1、
在左子树,这时左子树的点权值和等于
,计算没问题。
2、
在右子树,这时
对
的贡献已经包含在 右子树的
中,只要计算对
的贡献即可,而左子树的点权值和 =
,也不会有问题。
剩下的就是改改模板,套一套
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 10;
typedef long long ll;
#define pii pair<int,int>
#define fir first
#define sec second
int n,m,k,cnt;
map<int,int> mp[maxn];
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的操作以及数组
#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],sz[maxn],col[maxn],val[maxn]; //每个节点的颜色和权值
int sumcol[maxn],sumcol2[maxn]; // 维护splay子树和虚子树的 颜色 之和 ,黑色 col 为 1,白色 col 为 0
ll valson[maxn],lmn[maxn],rmn[maxn]; // splay 上每个节点对应子树的最左端点的答案和最右端点的答案
ll dis[maxn];
// sum维护 splay 子树节点权值之和 , valson[maxn] 维护虚子树的贡献
inline bool get(int x) {
return ch[f[x]][1] == x;
}
void init() {
for (int i = 1; i <= n; i++) {
sz[i] = 1;
col[i] = 0;
lmn[i] = rmn[i] = 0;
sumcol[i] = sumcol2[i] = 0;
dis[i] = val[i] = 0;
valson[i] = 0;
}
}
void pushdown(int x) {
if (tag[x]) {
if (ls) {
tag[ls] ^= 1;
swap(ch[ls][0],ch[ls][1]);
swap(lmn[ls],rmn[ls]);
}
if (rs) {
tag[rs] ^= 1;
swap(ch[rs][0],ch[rs][1]);
swap(lmn[rs],rmn[rs]);
}
tag[x] = 0;
}
}
inline void pushup(int x) {
if (x) {
sumcol[x] = sumcol2[x] + col[x] + sumcol[ls] + sumcol[rs];
dis[x] = dis[ls] + dis[rs] + val[x];
lmn[x] = lmn[ls] + valson[x] + lmn[rs] + (sumcol2[x] + sumcol[rs] + col[x]) * (dis[ls] + val[x]);
rmn[x] = rmn[ls] + valson[x] + rmn[rs] + (sumcol2[x] + sumcol[ls] + col[x]) * (dis[rs] + val[x]);
}
}
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];
for (int i = top; i >= 1; i--) pushdown(sta[i]);
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);
valson[x] += lmn[ch[x][1]];
valson[x] -= lmn[lst];
sumcol2[x] += sumcol[ch[x][1]];
sumcol2[x] -= sumcol[lst];
ch[x][1] = lst;
pushup(x);
lst = x; x = f[x];
}
}
inline void move_to_root(int x) {
access(x); splay(x);
tag[x] ^= 1;
swap(ch[x][0],ch[x][1]);
swap(lmn[x],rmn[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 split(int x,int y) {
move_to_root(x); access(y); splay(y);
}
inline void link(int x,int y) {
move_to_root(x); move_to_root(y); //由于 sz 维护的是辅助树中子树结点个数,不再是 splay 中子树结点个数
f[x] = y; splay(x); //连边操作后,若 y 不是根,则 y 的所有父亲都要更新,不如先将 y 移到根
valson[y] += lmn[x];
sumcol2[y] += sumcol[x];
}
inline void cut(int x,int y) {
split(x,y);
ch[y][0] = f[x] = 0;
pushup(y);
}
}tree;
char op[10];
int id,x,y,u,v,w;
int main() {
n = read(); m = read(); k = read();
for (int i = 1; i <= m; i++) {
u = read(); v = read(); w = read();
if (u > v) swap(u,v);
if (!mp[u][v]) mp[u][v] = ++cnt;
int p = mp[u][v] + n;
tree.dis[p] = tree.val[p] = w;
tree.link(u,p);
tree.link(v,p);
}
while (k--) {
scanf("%s",op);
if (op[0] == 'L') {
u = read(); v = read(); w = read();
if (u > v) swap(u,v);
if (!mp[u][v]) mp[u][v] = ++cnt;
int p = mp[u][v] + n;
tree.dis[p] = tree.val[p] = w;
tree.link(u,p);
tree.link(v,p);
} else if (op[0] == 'C') {
u = read(); v = read();
if (u > v) swap(u,v);
int p = mp[u][v] + n;
tree.cut(u,p); tree.cut(v,p);
} else if (op[0] == 'F') {
u = read();
tree.access(u); tree.splay(u);
tree.col[u] ^= 1;
//tree.pushup(u); 这个位置在顶端可以不用pushup,减少pushup的次数,减少常数
} else {
u = read();
tree.access(u); tree.splay(u);
printf("%lld\n",tree.rmn[u]);
}
}
return 0;
}