题解:

就是按照常规的合并

期望有一点麻烦

首先计算全部的和

再减去有多少种

具体看看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. 少走冤枉路!带你走过SNMP的那些坑

    SNMP(Simple Network Management Protocol)即简单网络管理协议,是在网络与系统监控领域中,最常使用的一种数据采集技术.尽管这个协议非常简单,但在大规模IT环境监测中 ...

  2. 006-spring cache-缓存实现-01-原生实现

    一.原生实现 1.1.pom <!-- 缓存 --> <dependency> <groupId>org.springframework.boot</grou ...

  3. python3 requests 进行接口测试、爬虫使用总结

    Requests 是第三方模块,如果要使用的话需要导入.Requests也可以说是urllib模块的升级版,使用上更方便. 这是使用urllib的例子. import urllib.request i ...

  4. 3.2 Templates -- The Application Template

    1. 当你的应用程序启动时application模板是默认被渲染的的模板. 2. 你应该把你的header, footer和其他任何的装饰内容放到这里.此外,你应该有至少一个{{outlet}}:它是 ...

  5. hdu5057 分块处理,当数值大于数据范围时树状数组 真是巧 将大数据分为小数据来处理

    这题说的给了100000个数有100000次操作 询问 L和R 区间内 在D位上为P的个数,用树状数组存 要开[10][10][100000]的int 开不了但是能开 这么大的unsign short ...

  6. linux及安全第五周总结——20135227黄晓妍

    (注意:本文总结备份中有较多我手写笔记的图片,其中重要的部分打出来了.本文对分析system_call对应的汇编代码的工作过程,系统调用处理过程”的理解,以及流程图都写在实验部分.) 实验部分 使用g ...

  7. 20145204《java程序设计》课程总结

    ---恢复内容开始--- 20145204<java程序设计>课程总结 每周读书笔记链接汇总: · 20145204<java程序设计>第一周总结 · 20145204< ...

  8. rm删除某个文件之外的文件

    1.列出排除hns_dsaf_mac.c文件之外的.c文件 ls drivers/net/ethernet/hisilicon/hns/*.c |egrep -v '(hns_dsaf_mac.c)' ...

  9. Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  10. ThinkPHP 表单自动验证运用

    使用TP 3.2框架 public function add_post(){ //验证规则 $rule=array( array('name','require','请输入姓名',1),//必须验证n ...