题解:

就是按照常规的合并

期望有一点麻烦

首先计算全部的和

再减去有多少种

具体看看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. Python知识总汇

    一.python基础 python基础 python编码问题 逻辑运算 二.python数据类型 二.python数据类型 三.IO(文件处理) 三.IO(文件处理) 四.函数 函数基础 名称空间与作 ...

  2. EA类图与代码同步

    画了一段时间的图,愈发感觉到EA是一个强大的软件,而不不过一个绘图工具那么简单. . 随着学习时间的延长.如今写程序并不能像曾经一样随心所欲,想到什么就敲什么了,而是要先绘图(也就是理需求和思路的过程 ...

  3. MVC和观察者模式

    用观察者模式实现MVC框架! http://wenku.baidu.com/view/eff8bab069dc5022aaea0007.html 写的不错! Observer和ConcreteObse ...

  4. python之redis模块

    一.redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...

  5. Spring中基于Java的配置@Configuration和@Bean用法

    spring中为了减少xml中配置,可以声明一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...

  6. PAT 1043 Is It a Binary Search Tree[二叉树][难]

    1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  7. array2xml xml2array

    array2xml/**     *     * 将简单数组转化为简单的xml     * @param string $data  要进行转化的数组     * @param string $tag ...

  8. C++ builder 书籍推荐

    china-pub网上书店c++builder书籍专区,本专区专门为c++builder学习者提供目前最为畅销实用的c++builder技术书籍,通过对本专区c++builder书籍的了解,让您学习c ...

  9. 1:1 Struts2概述

    jar包下载

  10. 算法第四版 在Linux 中调用Algs4库

    一: 搭建Java 环境   : 确认版本: 1.8及以上 [username:~/] javac -version javac 1.8.0_111 [username:~/] java -versi ...