loj #558 我们的 CPU 遭到攻击
this
\text{Solution:}
跟qtree5差不多吧。
首先肯定是LCT.
记录下lans,表示从右子树跳到左子树的贡献。
再记录下rans,表示从左子树跳到右子树的贡献。
PS:这里跳过去的点不止,例如lans,我们钦定跳过去的点应该是右子树的所有点加根再加虚子树。
考虑下查询,先make_root(u),此时u是深度最小的了,查询lans即可。
考虑push_up.
void push_up(int x) {
sumc[x]=col[x]+sumc[ls]+sumc[rs]+sumc2[x];
dis[x]=val[x]+dis[ls]+dis[rs];
lans[x]=lans[ls]+ans[x]+lans[rs]+(sumc[rs]+sumc2[x]+col[x])*(dis[ls]+val[x]); //从右边走到左边
rans[x]=rans[ls]+ans[x]+rans[rs]+(sumc[ls]+sumc2[x]+col[x])*(dis[rs]+val[x]);
}
其中sumc指x子树内的黑点数量,dis指x子树内的路径和,lans[ls]+lans[rs]表示左右儿子的右子树跳到左边去!ans表示虚子树的贡献,大可认为是原来的右儿子,但现在不是了的贡献。
那么就剩下以x为根的lans贡献了。
我们考虑如何让右子树都跳到左子树。
是不是跳到根再跳过去就好了?
因为我们这里用点权表示边权,所以就是跳到根节点。
答案就是 (dis[ls]+val[x])。
最后乘上跳过去的黑点个数即可。
rans同理。
对于access,钦定rs为原来的实儿子,y为将来的实儿子。
sumc2[x]+=sumc[rs]-sumc[y];
ans[x]+=lans[rs]-lans[y];
首先sumc2很好解释。
对于ans,我们发现access的更改都是与右子树有关,所以答案自然也就是从右子树跳过去了。
\text{Code:}
#include <bits/stdc++.h>
#define N (int)(4e5+5)
#define ls ch[0][x]
#define rs ch[1][x]
#define int long long
using namespace std;
bool rev[N];
int fa[N],ch[2][N],rans[N],lans[N],dis[N],sumc[N],sumc2[N],ans[N],col[N],val[N];
bool ident(int x) {
return ch[1][fa[x]]==x;
}
bool check_root(int x) {
return ch[0][fa[x]]==x||ch[1][fa[x]]==x;
}
void push_r(int x) {
swap(ls,rs); swap(lans[x],rans[x]); rev[x]^=1;
}
void push_down(int x) {
if(rev[x]) {
if(ls) push_r(ls);
if(rs) push_r(rs);
rev[x]=0;
}
}
void push_up(int x) {
sumc[x]=col[x]+sumc[ls]+sumc[rs]+sumc2[x];
dis[x]=val[x]+dis[ls]+dis[rs];
lans[x]=lans[ls]+ans[x]+lans[rs]+(sumc[rs]+sumc2[x]+col[x])*(dis[ls]+val[x]); //从右边走到左边
rans[x]=rans[ls]+ans[x]+rans[rs]+(sumc[ls]+sumc2[x]+col[x])*(dis[rs]+val[x]);
}
void connect(int x,int f,int how) {
ch[how][f]=x; fa[x]=f;
}
void rotate(int x) {
int y=fa[x],z=fa[y],k=ident(x);
connect(ch[k^1][x],y,k); fa[x]=z;
if(check_root(y)) ch[ident(y)][z]=x;
connect(y,x,k^1);
push_up(y); push_up(x);
}
void push_all(int x) {
if(check_root(x)) push_all(fa[x]);
push_down(x);
}
void splay(int x) {
push_all(x);
while(check_root(x)) {
int y=fa[x];
if(check_root(y)) ident(x)^ident(y)?rotate(x):rotate(y);
rotate(x);
}
push_up(x);
}
void access(int x) {
for(int y=0;x;y=x,x=fa[x]) {
splay(x);
sumc2[x]+=sumc[rs]-sumc[y];
ans[x]+=lans[rs]-lans[y];
rs=y;
push_up(x);
}
}
int fdroot(int x) {
access(x); splay(x);
while(ls) push_down(ls),x=ls;
splay(x);
return x;
}
void make_root(int x) {
access(x); splay(x); push_r(x);
}
void link(int x,int y) {
make_root(x);
make_root(y);
sumc2[y]+=sumc[x];
ans[y]+=lans[x];
fa[x]=y; push_up(y);
}
void cut(int x,int y) {
make_root(x);
if(fdroot(y)!=x||fa[y]!=x||ch[0][y]) return;
fa[y]=ch[1][x]=0;
push_up(x);
}
int rd() {
int f=1,sum=0; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) {sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return sum*f;
}
map<int,int>mp;
int n,m,q,u[N],v[N];
signed main() {
n=rd(); m=rd(); q=rd();
for(int i=1;i<=m;i++) {
u[i]=rd(); v[i]=rd();
if(u[i]>v[i]) swap(u[i],v[i]);
mp[u[i]*(n+1)+v[i]]=i+n;
val[i+n]=rd();
link(u[i],i+n);
link(v[i],i+n);
}
int tot=m,x,y,z;
char op;
while(q--) {
cin>>op;
if(op=='L') {
u[++tot]=rd(); v[tot]=rd();
if(u[tot]>v[tot]) swap(u[tot],v[tot]);
val[tot+n]=rd(); mp[u[tot]*(n+1)+v[tot]]=tot+n;
link(u[tot],tot+n);
link(v[tot],tot+n);
} else if(op=='C') {
x=rd(); y=rd();
if(x>y) swap(x,y);
int tmp=mp[x*(n+1)+y];
cut(x,tmp); cut(y,tmp);
} else if(op=='F') {
x=rd(); make_root(x);
col[x]^=1; push_up(x);
} else {
x=rd();
make_root(x);
printf("%lld\n",lans[x]);
}
}
return 0;
}