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

 
恶心的动态树上维护各种信息。
不难发现ans=ΣAi*i*(len-i+1)。
我们在splay树上维护几个值:
sumv=ΣAi*i*(len-i+1)
lsum=ΣAi*i
rsum=ΣAi*(len-i+1)
sum=ΣAi
不难维护者几个变量的关系,打懒标记时快速算一下Σi*(len-i+1)和Σi就行了。
注意flip时要交换两子树的lsum和rsum。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#define lc ch[x][0]
#define rc ch[x][1]
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
const int BufferSize=<<;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=,f=;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-;
for(;isdigit(c);c=Getchar()) x=x*+c-'';
return x*f;
}
typedef long long ll;
const int maxn=;
ll f[maxn],sumv[maxn],sum[maxn],lsum[maxn],rsum[maxn],s[maxn],addv[maxn],val[maxn];
int pre[maxn],fa[maxn],ch[maxn][],flip[maxn];
void Add(int x,ll v) {
if(!v||!x) return;
sumv[x]+=v*f[s[x]];
lsum[x]+=v*(+s[x])*s[x]/;
rsum[x]+=v*(+s[x])*s[x]/;
sum[x]+=v*s[x];
}
void maintain(int x) {
if(!x) return;
s[x]=s[lc]+s[rc]+;
sum[x]=sum[lc]+sum[rc]+val[x];
sumv[x]=sumv[lc]+sumv[rc]+lsum[lc]*(s[rc]+)+rsum[rc]*(s[lc]+)+val[x]*(s[lc]+)*(s[rc]+);
lsum[x]=lsum[lc]+lsum[rc]+val[x]*(s[lc]+)+sum[rc]*(s[lc]+);
rsum[x]=rsum[rc]+rsum[lc]+val[x]*(s[rc]+)+sum[lc]*(s[rc]+);
Add(x,addv[x]);
}
void pushdown(int x) {
if(flip[x]) {
swap(lc,rc);
swap(lsum[lc],rsum[lc]);
swap(lsum[rc],rsum[rc]);
flip[lc]^=;flip[rc]^=;
flip[x]=;
}
if(addv[x]) {
val[x]+=addv[x];addv[lc]+=addv[x];addv[rc]+=addv[x];
Add(lc,addv[x]);Add(rc,addv[x]);addv[x]=;
}
}
void rotate(int x) {
int y=pre[x],z=pre[y],d=ch[y][]==x;
ch[y][d^]=ch[x][d];pre[ch[x][d]]=y;
ch[z][ch[z][]==y]=x;pre[x]=z;
ch[x][d]=y;pre[y]=x;maintain(y);
}
int S[maxn],top;
void splay(int x) {
for(int i=x;i;i=pre[i]) S[++top]=i;
if(top!=) fa[x]=fa[S[top]],fa[S[top]]=;
while(top) pushdown(S[top--]);
while(pre[x]) rotate(x);
maintain(x);
}
void access(int x) {
for(int y=;x;x=fa[x]) {
splay(x);pre[ch[x][]]=;fa[ch[x][]]=x;
ch[x][]=y;pre[y]=x;maintain(y=x);
}
}
void makeroot(int x) {
access(x);splay(x);flip[x]^=;
maintain(x);
}
int find(int x) {
access(x);splay(x);
while(ch[x][]) x=ch[x][];
return x;
}
void link(int x,int y) {
makeroot(x);fa[x]=y;
}
void cut(int x,int y) {
makeroot(x);access(y);splay(y);
if(s[y]==) {
pre[ch[y][]]=;ch[y][]=;
maintain(y);
}
}
ll gcd(ll x,ll y) {return !y?x:gcd(y,x%y);}
void query(int x,int y) {
makeroot(x);access(y);splay(y);
ll len=s[y]-;len=(len+)*(len+)/;
ll ans=sumv[y],t=gcd(len,ans);
printf("%lld/%lld\n",ans/t,len/t);
}
void update(int x,int y,ll v) {
makeroot(x);access(y);splay(y);
addv[y]+=v;Add(y,v);
}
int main() {
int n=read(),m=read();
f[]=;
rep(i,,n) f[i]=f[i-]+(ll)i*(i-)/+i;
rep(i,,n) val[i]=read();
rep(i,,n-) link(read(),read());
while(m--) {
int t=read(),x=read(),y=read();
if(t==) if(find(x)==find(y)) cut(x,y);
if(t==) if(find(x)!=find(y)) link(x,y);
if(t==) {
ll v=read();
if(find(x)==find(y)) update(x,y,v);
}
if(t==) {
if(find(x)==find(y)) query(x,y);
else puts("-1");
}
}
return ;
}

BZOJ3091: 城市旅行的更多相关文章

  1. 【LCT】BZOJ3091 城市旅行

    3091: 城市旅行 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1927  Solved: 631[Submit][Status][Discuss ...

  2. BZOJ3091 城市旅行 LCT

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3091 题意概括 鉴于本人语文不好,此题的描述原题很清晰,废话不多,请看原题. 可怕,原题是图片,不 ...

  3. BZOJ3091城市旅行——LCT区间信息合并

    题目描述 输入 输出 样例输入 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 样例输出 16/3 6/1 提示 对于所有数据满足 1& ...

  4. BZOJ3091: 城市旅行(LCT,数学期望)

    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 ...

  5. bzoj3091 城市旅行 LCT + 区间合并

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3091 题解 调了整个晚自习才调出来的问题. 乍一看是个 LCT 板子题. 再看一眼还是个 LC ...

  6. 【BZOJ3091】城市旅行 LCT

    [BZOJ3091]城市旅行 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 ...

  7. BZOJ 3091: 城市旅行 [LCT splay 期望]

    3091: 城市旅行 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1454  Solved: 483[Submit][Status][Discuss ...

  8. luogu P4842 城市旅行

    嘟嘟嘟 好题,好题 刚开始突发奇想写了一个\(O(n ^ 2)\)暴力,结果竟然过了?!后来才知道是上传题的人把单个数据点开成了10s-- 不过不得不说我这暴力写的挺好看的.删边模仿链表删边,加边的时 ...

  9. 【bzoj3091】 城市旅行

    http://www.lydsy.com/JudgeOnline/problem.php?id=3091 (题目链接) 题意 给出一棵无根树,维护四个操作.link,cut,路径加法,路径期望查询. ...

随机推荐

  1. WAF绕过神器 (过安全狗、智创SQL注入)

    WAF绕过神器 (过安全狗.智创SQL注入) 发布时间:-- :10文章来源:网络文章作者:panni007 点击次数: 次 分享到: QQ空间 QQ微博 新浪微博 开心网 人人网 摘要:起因: by ...

  2. 用chrome模拟微信浏览器访问需要OAuth2.0网页授权的页面

    现在很流行微信网页小游戏,用html5制作的小游戏移过来,可以放到微信浏览器中打开,关键是可以做成微信分享朋友圈的形式,大大提高游戏的传播,增强好友的游戏互动. 微信浏览器中打开网页游戏效果还不错,对 ...

  3. WCDMA是什么意思?CDMA是什么意思?GSM是什么意思

    有些朋友在购买3G智能手机的时候会遇到这样的困惑,为什么相同的手机会有不同手机网络制式之分呢?有的支持WCDMA/GSM,有的支持CDMA/GSM,到底自己应该选购哪一种手机好呢?WCDMA是什么意思 ...

  4. POJ 2386

    http://poj.org/problem?id=2386 这个题目与那个POJ 1562几乎是差不多的,只不过那个比这个输入要复杂一些 #include <stdio.h> #incl ...

  5. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. HDU1850 Being a Good Boy in Spring Festival(博弈)

    Being a Good Boy in Spring Festival Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I ...

  7. solr6.0学习

    solr6.0学习(一)环境搭建准备工作:目前最新版本6.0.下载solr 6.0:Solr6.0下载JDK8 下载jdk1.8:jdk1.8[solr6.0是基于jdk8开发的]tomcat8.0 ...

  8. SharedPreferences&SQLite比较

    SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置比如窗口状态,一般在Activity中 重载窗口状态onSaveInstanceState保存一般使 ...

  9. 【现代程序设计】加分作业1-对Stack的理解

    要求:本次加分作业是要阅读这篇文章“ Stack的三种含义”,以及文章下方的评论,然后做出总结. ----------------------------------------华丽的分割线----- ...

  10. BZOJ 3224: Tyvj 1728 普通平衡树 treap

    3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...