BZOJ2752: [HAOI2012]高速公路(road)(洛谷P4412)

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

LCT

BZOJ题目传送门
洛谷题目传送门

这道题的弱化版,用线段树维护就可以了。但我懒所以就直接双倍经验了。

代码:

#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
#define F inline
#define V F void
using namespace std;
typedef 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)&&!isupper(ch)){ if (ch=='-') f=-1; ch=readc(); }
	if (isupper(ch)) return ch;
	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+=sz*(sz+1)/2*(sz+2)/3*w;
}
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); }
V lnk(int x,int y){ mkrt(x),t[x].fa=y; }
int main(){
	n=_read(),m=_read();
	for (int i=1;i<n;i++) lnk(i,i+1);
	for (int f,x,y,d;m;m--){
		f=_read(),x=_read(),y=_read()-1,sprt(x,y);
		if (f=='C') add2(y,_read());
		else{
			LL ans1=t[y].xs,ans2=1ll*t[y].sz*(t[y].sz+1)>>1,r=gcd(ans1,ans2);
			printf("%lld/%lld\n",ans1/r,ans2/r);
		}
	}
	return 0;
}

猜你喜欢

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