bzoj 3091 城市旅行

3091: 城市旅行

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 1697   Solved: 565
[ Submit][ Status][ Discuss]

Description

Input

Output

Sample Input

4 5
1 3 2 5
1 2
1 3
2 4
4 2 4
1 2 4
2 3 4
3 1 4 1
4 1 4

Sample Output

16/3
6/1

HINT

对于所有数据满足 1<=N<=50,000 1<=M<=50,000 1<=Ai<=10^6 1<=D<=100 1<=U,V<=N

Source






【分析】

Congratulation!bzoj再度爆炸!稳定点会怎样哦!宝宝要做题(ㄒoㄒ)


嗯...直接放出QQQ大爷的题解   http://blog.csdn.net/popoqqq/article/details/40823659

注意一点,翻转区间的时候也要翻转 lsum[x]和rsum[x]...

本题的翻转标记必须立即生效,因为反转标记还会影响lsum和rsum

由于判断opt==3的时候直接忽略了后面读入的w所以WA到cry


【代码】

#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=50005;
int n,m,w,opt,top;
int ch[mxn][2],f[mxn],rev[mxn],st[mxn];
ll size[mxn],L[mxn],R[mxn],sum[mxn],exp[mxn],val[mxn],add[mxn];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f;
}
inline bool isroot(int x) {return ch[f[x]][0]!=x && ch[f[x]][1]!=x;}
inline int get(int x) {return ch[f[x]][1]==x;}
inline void update(int x)
{
	int l=ch[x][0],r=ch[x][1];
	size[x]=size[l]+size[r]+1;
	sum[x]=sum[l]+sum[r]+val[x];
	L[x]=L[l]+(size[l]+1)*(val[x]+sum[r])+L[r];
	R[x]=R[r]+(size[r]+1)*(val[x]+sum[l])+R[l];
	exp[x]=exp[l]+exp[r]+(size[r]+1)*L[l]+(size[l]+1)*R[r]+(size[l]+1)*(size[r]+1)*val[x];
}
inline void ope(int x,ll c)
{
	if(!x) return;
	int l=ch[x][0],r=ch[x][1];
	sum[x]+=size[x]*c;
	val[x]+=c,add[x]+=c;
	L[x]+=c*size[x]*(size[x]+1)/2;
	R[x]+=c*size[x]*(size[x]+1)/2;
	exp[x]+=c*size[x]*(size[x]+1)*(size[x]+2)/6;
}
inline void reverse(int x)
{
	if(!x) return;
	rev[x]^=1;
	swap(L[x],R[x]);
	swap(ch[x][0],ch[x][1]);
}
inline void pushdown(int x)
{
	int l=ch[x][0],r=ch[x][1];
	if(rev[x]) reverse(ch[x][0]),reverse(ch[x][1]);
	if(add[x]) ope(ch[x][0],add[x]),ope(ch[x][1],add[x]);
	rev[x]=add[x]=0;
}
inline void rotate(int x)
{
	pushdown(x);
	int fa=f[x],fafa=f[fa],which=get(x);
	if(!isroot(fa)) ch[fafa][ch[fafa][1]==fa]=x;f[x]=fafa;
	ch[fa][which]=ch[x][which^1],f[ch[fa][which]]=fa;
	ch[x][which^1]=fa,f[fa]=x;
	update(fa),update(x);
}
inline void splay(int x)
{
	st[top=1]=x;
	for(int i=x;!isroot(i);i=f[i]) st[++top]=f[i];
	for(int i=top;i;i--) pushdown(st[i]);
	for(int fa;!isroot(x);rotate(x))
	  if(!isroot(fa=f[x])) rotate(get(x)==get(fa)?fa:x);
}
inline void access(int x)
{
	for(int y=0;x;y=x,x=f[x])
	  splay(x),ch[x][1]=y,update(x);
}
inline void makeroot(int x)
{
	access(x),splay(x),reverse(x);
}
inline void split(int x,int y)
{
	makeroot(x),access(y),splay(y);
}
inline int find(int x)
{
	access(x),splay(x);
	while(ch[x][0]) x=ch[x][0];
	return x;
}
inline void link(int x,int y)
{
	makeroot(x),f[x]=y;
//	int i;fo(i,0,n) printf("father[%d]=%d\n",i,f[i]);
}
inline void cut(int x,int y)
{
	split(x,y);
	ch[y][0]=f[x]=0;
}
inline ll gcd(ll x,ll y) {return y==0?x:gcd(y,x%y);}
inline void solve(int x,int y)
{
	if(find(x)!=find(y))
	{
		printf("-1\n");return;
	}
	split(x,y);
	ll a=exp[y],b=size[y]*(size[y]+1)/2;
	ll tmp=gcd(a,b);
	printf("%lld/%lld\n",a/tmp,b/tmp);
}
int main()
{
	int i,j,k,u,v;
	n=read(),m=read();
	fo(i,1,n)
	  size[i]=1,L[i]=R[i]=sum[i]=exp[i]=val[i]=read();
	fo(i,2,n)
	  u=read(),v=read(),link(u,v);
	while(m--)
	{
		opt=read(),u=read(),v=read();
		if(opt==1 && find(u)==find(v)) cut(u,v);
		if(opt==2 && find(u)!=find(v)) link(u,v);
		if(opt==3)
		{
			w=read();
			if(find(u)==find(v)) split(u,v),ope(v,(ll)w);
		}
		if(opt==4) solve(u,v);
	}
	return 0;
}
/*
4 1
1 3 2 5
1 2
1 3
2 4
4 3 2
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值