题解:

就是按照常规的合并

期望有一点麻烦

首先计算全部的和

再减去有多少种

具体看看http://blog.csdn.net/PoPoQQQ/article/category/2542261这个博客吧

代码:

#include<bits/stdc++.h>
using namespace std;
#define pa t[x].fa
#define lc t[x].ch[0]
#define rc t[x].ch[1]
const int N=5e4+;
typedef long long ll;
int read()
{
char c=getchar();int x=,f=;
while(c<''||c>''){if (c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
struct node
{
int ch[],fa,rev;
ll add,lsum,rsum,sum,exp,w,size;
}t[N];
int wh(int x){return t[pa].ch[]==x;}
int isRoot(int x){return t[pa].ch[]!=x&&t[pa].ch[]!=x;}
void update(int x)
{
t[x].size=t[lc].size+t[rc].size+;
t[x].sum=t[lc].sum+t[rc].sum+t[x].w;
t[x].lsum=t[lc].lsum+t[x].w*(t[lc].size+)+t[rc].lsum+t[rc].sum*(t[lc].size+);
t[x].rsum=t[rc].rsum+t[x].w*(t[rc].size+)+t[lc].rsum+t[lc].sum*(t[rc].size+);
t[x].exp=t[lc].exp+t[rc].exp
+t[lc].lsum*(t[rc].size+)+t[rc].rsum*(t[lc].size+)
+t[x].w*(t[lc].size+)*(t[rc].size+);
}
ll cal1(ll x){return x*(x+)/;}
ll cal2(ll x){return x*(x+)*(x+)/;}
void paint(int x,ll d)
{
t[x].w+=d;
t[x].add+=d;
t[x].sum+=d*t[x].size;
t[x].lsum+=d*cal1(t[x].size);
t[x].rsum+=d*cal1(t[x].size);
t[x].exp+=d*cal2(t[x].size);
}
void rever(int x)
{
swap(lc,rc);
swap(t[x].lsum,t[x].rsum);
t[x].rev^=;
}
void pushDown(int x)
{
if (t[x].rev)
{
rever(lc);
rever(rc);
t[x].rev=;
}
if (t[x].add)
{
paint(lc,t[x].add);
paint(rc,t[x].add);
t[x].add=;
}
}
void rotate(int x)
{
int f=t[x].fa,g=t[f].fa,c=wh(x);
if (!isRoot(f)) t[g].ch[wh(f)]=x;t[x].fa=g;
t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f;t[f].fa=x;
update(f);update(x);
}
int st[N],top;
void splay(int x)
{
top=;st[++top]=x;
for (int i=x;!isRoot(i);i=t[i].fa) st[++top]=t[i].fa;
for (int i=top;i>=;i--) pushDown(st[i]);
for (;!isRoot(x);rotate(x))
if (!isRoot(pa)) rotate(wh(x)==wh(pa)?pa:x);
}
void Access(int x)
{
for (int y=;x;y=x,x=pa)
{
splay(x);
rc=y;
update(x);
}
}
void MakeR(int x){Access(x);splay(x);rever(x);}
int FindR(int x){Access(x);splay(x);while(lc) x=lc;return x;}
void Link(int x,int y){MakeR(x);t[x].fa=y;}
void Cut(int x,int y)
{
MakeR(x);Access(y);splay(y);
t[y].ch[]=t[x].fa=;
update(y);
}
void Add(int x,int y,int d)
{
if (FindR(x)!=FindR(y)) return;
MakeR(x);Access(y);splay(y);
paint(y,d);
}
ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);}
void Que(int x,int y)
{
if (FindR(x)!=FindR(y)){puts("-1");return;}
MakeR(x);Access(y);splay(y);
ll a=t[y].exp,b=t[y].size*(t[y].size+)/;
ll g=gcd(a,b);
printf("%lld/%lld\n",a/g,b/g);
}
int n,Q,a,op,x,y,d;
int main()
{
n=read();Q=read();
for (int i=;i<=n;i++)
{
a=read();
t[i].size=;
t[i].w=t[i].lsum=t[i].rsum=t[i].sum=t[i].exp=a;
}
for (int i=;i<=n-;i++) x=read(),y=read(),Link(x,y);
while(Q--)
{
op=read();x=read();y=read();
if (op==) if (FindR(x)==FindR(y)) Cut(x,y);
if (op==) if (FindR(x)!=FindR(y)) Link(x,y);
if (op==) d=read(),Add(x,y,d);
if (op==) Que(x,y);
}
}

bzoj3901的更多相关文章

  1. 题解-bzoj3901 棋盘游戏

    2019年第一篇文章 (。・∀・)ノ゙ Problem bzoj无良权限题,拿学长的号交的 题目概要:给定一个\(n\times n\)的矩阵.令\(x=\frac {n+1}2\).可以进行任意次以 ...

随机推荐

  1. 双态运维分享之二: 服务型CMDB的消费场景

    近年来,CMDB在IT运维管理中的价值逐步得到认可,使用CMDB的期望值也日益增长.然而,CMDB实施和维护的高成本却一直是建设者们的痛点.那么今天,我们来探讨一下如何通过消费来持续驱动CMDB的逐步 ...

  2. mysql 数据操作 单表查询

    单表查询的语法 distinct 去重 SELECT 字段1,字段2... FROM 表名 库.表名 WHERE 条件 过滤 符合条件的 GROUP BY field 分组条件 HAVING 筛选 过 ...

  3. Flask系列(七)Flask中的wtforms使用

    一.简单介绍flask中的wtforms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装: pip3 install wtforms 二.简单使用wtfo ...

  4. spark 调优概述

    分为几个部分: 开发调优.资源调优.数据倾斜调优.shuffle调优 开发调优: 主要包括这几个方面 RDD lineage设计.算子的合理使用.特殊操作的优化等 避免创建重复的RDD,尽可能复用同一 ...

  5. Jmeter之Constant Timer与constant throughput timer的区别

    当放置Constant Timer于两个http请求之间,那么它代表的含义是:在上一个请求发出至完成后, 开始Contant Timer指定的时间,最后再发出第二个请求.它并不是代表两个请求之间的发送 ...

  6. jQuery获取属性

    jQuery在获取jQuery对象的属性时,出现attr()获取不到的情况,此时,请使用prop()获取 如下为经常用到的: var oHtml=$(this).prop("outerHTM ...

  7. Java IO的Reactor模式

    1.    Reactor出现的原因 Reator模式是大多数IO相关组件如Netty.Redis在使用时的IO模式,为什么需要这种模式,如何设计来解决高性能并发的呢? 最最原始的网络编程思路就是服务 ...

  8. myeclips破解

    MyEclipse官方安装文件,下载地址 http://www.jb51.net/softs/150886.html破解补丁http://www.jb51.net/softs/150887.html ...

  9. mongodb的安装和权限管理

    mongodb4.0已经发布,但是鉴于线上环境更多的是使用旧版本的mongodb,我们这里使用的mongodb3.4版本. 官网下载地址为:https://fastdl.mongodb.org/lin ...

  10. bzoj1605 / P2905 [USACO08OPEN]农场危机Crisis on the Farm

    P2905 [USACO08OPEN]农场危机Crisis on the Farm 发现总步数$k<=30$,考虑用$k$瞎搞 设$f[u][i][j]$表示已经吹$u$次哨,全体奶牛向右走$i ...