BZOJ3091: 城市旅行

版权声明:蒟蒻Blog随意转载 https://blog.csdn.net/a1799342217/article/details/82778222

LCT

题目传送门

Orz PoPoQQQ大爷

前三个操作都是板子,关键是第4个操作的维护。分母为 ( s i z e 2 ) size\choose2 ,那么只要维护分子即可。对于大小为 s i z e size 的节点,分子即为 i = 1 s i z e ( s i z e i + 1 ) × i × w i \sum_{i=1}^{size}(size-i+1)\times i\times w_i

我们把路径拎出来拍平成一个区间,可以得到左子树的贡献从 i = 1 l s i z e ( l s i z e i + 1 ) × i × w i \sum_{i=1}^{lsize}(lsize-i+1)\times i\times w_i 变成 i = 1 l s i z e ( s i z e i + 1 ) × i × w i \sum_{i=1}^{lsize}(size-i+1)\times i\times w_i ,即多了 ( r s i z e + 1 ) × ( i = 1 l s i z e i × w i ) (rsize+1)\times(\sum_{i=1}^{lsize}i\times w_i) ,那么我们记录 l s = i = 1 l s i z e i × w i ls=\sum_{i=1}^{lsize}i\times w_i ,右子树同理。这样就可以维护了。

但是我们还有路径加这个操作,那么主要就是计算 i = 1 s i z e i × ( s i z e i + 1 ) \sum_{i=1}^{size}i\times(size-i+1) 。而这个东西就等于 s i z e × ( s i z e + 1 ) × ( s i z e + 2 ) / 6 size\times(size+1)\times(size+2)/6 。证明的话就直接把 ( s i z e 1 ) i = 1 s i z e i i = 1 s i z e i 2 (size-1)\sum_{i=1}^{size}i-\sum_{i=1}^{size}i^2 化简就好了。这两个式子是等价的。

还有就是判联通需要找根。有两种方法:一种直接跳,还有一种先access再splay,然后找到最深的左子树就是根。

代码:

#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 50005
#define F inline
#define V F void
using namespace std;
typedef unsigned long long LL;
struct tree{ int fa,f1,to[2]; LL f2,sz,s,x,ls,rs,xs; }t[N];
int n,m,tp,stk[N];
F char readc(){
	static char buf[100000],*l=buf,*r=buf;
	if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
	return l==r?EOF:*l++;
}
F int _read(){
	int x=0,f=1; char ch=readc();
	while (!isdigit(ch)){ if (ch=='-') f=-1; ch=readc(); }
	while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
	return x*f;
}
F LL gcd(LL a,LL b){ return !b?a:gcd(b,a%b); }
#define rt(x) (t[t[x].fa].to[0]!=x&&t[t[x].fa].to[1]!=x)
V pshp(int x){
	int l=t[x].to[0],r=t[x].to[1]; LL ls=t[l].sz+1,rs=t[r].sz+1;
	t[x].sz=ls+rs-1,t[x].s=t[l].s+t[r].s+t[x].x;
	t[x].ls=t[l].ls+t[r].ls+(t[r].s+t[x].x)*ls;
	t[x].rs=t[r].rs+t[l].rs+(t[l].s+t[x].x)*rs;
	t[x].xs=t[l].xs+t[r].xs+rs*t[l].ls+ls*t[r].rs+ls*rs*t[x].x;
}
V add1(int x){ swap(t[x].to[0],t[x].to[1]),swap(t[x].ls,t[x].rs),t[x].f1^=1; }
V add2(int x,LL w){
	if (!x) return; LL sz=t[x].sz;
	t[x].x+=w,t[x].f2+=w,t[x].s+=w*sz;
	t[x].ls+=w*sz*(sz+1)>>1;
	t[x].rs+=w*sz*(sz+1)>>1;
	t[x].xs+=w*sz*(sz+1)*(sz+2)/6;
}
V pshd(int x){
	int &l=t[x].to[0],&r=t[x].to[1];
	if (t[x].f1) add1(l),add1(r);
	if (t[x].f2) add2(l,t[x].f2),add2(r,t[x].f2);
	t[x].f2=t[x].f1=0;
}
V rtt(int x){
	int y=t[x].fa,z=t[y].fa,l=t[y].to[0]==x;
	if (!rt(y)) t[z].to[t[z].to[0]!=y]=x;
	t[x].fa=z,t[y].fa=x,t[t[x].to[l]].fa=y;
	t[y].to[l^1]=t[x].to[l],t[x].to[l]=y;
	pshp(y),pshp(x);
}
V splay(int x){
	for (int i=stk[tp=1]=x;!rt(i);i=t[i].fa) stk[++tp]=t[i].fa;
	while (tp) pshd(stk[tp--]);
	for (int y=t[x].fa,z=t[y].fa;!rt(x);rtt(x),y=t[x].fa,z=t[y].fa)
		if (!rt(y)) rtt(t[z].to[0]==y^t[y].to[0]==x?x:y);
}
V ccss(int x){ for (int i=0;x;i=x,x=t[x].fa) splay(x),t[x].to[1]=i,pshp(x); }
V mkrt(int x){ ccss(x),splay(x),add1(x); }
V sprt(int x,int y){ mkrt(x),ccss(y),splay(y); }
F int getfa(int x){ while (t[x].fa) x=t[x].fa; return x; }
V lnk(int x,int y){ mkrt(x),t[x].fa=y; }
V cut(int x,int y){
	if (x==y||getfa(x)!=getfa(y)) return; sprt(x,y);
	if (t[y].sz==2) t[x].fa=t[y].to[0]=0,pshp(y);
}
int main(){
	n=_read(),m=_read();
	for (int i=1,x;i<=n;i++) t[i].x=_read(),pshp(i);
	for (int i=1,x,y;i<n;i++) x=_read(),y=_read(),lnk(x,y);
	for (int f,x,y,d;m;m--){
		f=_read(),x=_read(),y=_read();
		if (f==1) cut(x,y); else if (f==2) { if (getfa(x)!=getfa(y)) lnk(x,y); }
		else if (f==3){ d=_read(); if (getfa(x)==getfa(y)) sprt(x,y),add2(y,d); }
		else{
			if (getfa(x)!=getfa(y)){ puts("-1"); continue; } sprt(x,y);
			LL ans1=t[y].xs,ans2=1ll*t[y].sz*(t[y].sz+1)>>1,r=gcd(ans1,ans2);
			printf("%llu/%llu\n",ans1/r,ans2/r);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/a1799342217/article/details/82778222