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

解题思路:

大爷比我讲得好到不知道哪里去了PoPoQQQ的博客

就是考虑一个点会被经过多少次*多少贡献,感觉这个好套路,线性求解不是问题,不会动态维护QAQ。

考虑一个点代表的子树信息内部处理完毕,处理左子树对右子树或右子树对左子树的影响,像分治的想法QAQ。

最后累计答案就累计左子树答案+右子树答案+自己答案+左对右答案+右对左答案就好了。

代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define lll tr[spc].ch[0]
#define rrr tr[spc].ch[1]
#define ls ch[0]
#define rs ch[1]
typedef long long lnt;
const int N=;
struct trnt{
int ch[];
int fa;
int lzt;
int wgt;
lnt add;
lnt val;
lnt ans;
lnt sum;
lnt lsum;
lnt rsum;
bool anc;
}tr[N];
int n,m;
bool whc(int spc)
{
return tr[tr[spc].fa].rs==spc;
}
void pushup(int spc)
{
if(!spc)
return ;
tr[spc].wgt=tr[lll].wgt+tr[rrr].wgt+;
tr[spc].sum=tr[lll].sum+tr[rrr].sum+tr[spc].val;
tr[spc].lsum=tr[lll].lsum+tr[rrr].lsum+tr[spc].val*(tr[lll].wgt+)+tr[rrr].sum*(tr[lll].wgt+);
tr[spc].rsum=tr[lll].rsum+tr[rrr].rsum+tr[spc].val*(tr[rrr].wgt+)+tr[lll].sum*(tr[rrr].wgt+);
tr[spc].ans=tr[lll].ans+tr[rrr].ans+tr[lll].lsum*(tr[rrr].wgt+)+tr[rrr].rsum*(tr[lll].wgt+)+tr[spc].val*(tr[lll].wgt+)*(tr[rrr].wgt+);
return ;
}
void Add(int spc,lnt v)
{
if(!spc)
return ;
tr[spc].add+=v;
tr[spc].val+=v;
tr[spc].sum+=v*tr[spc].wgt;
tr[spc].lsum+=v*(tr[spc].wgt)*(tr[spc].wgt+)/;
tr[spc].rsum+=v*(tr[spc].wgt)*(tr[spc].wgt+)/;
tr[spc].ans+=v*(tr[spc].wgt+)*(tr[spc].wgt+)*tr[spc].wgt/;
return ;
}
void trr(int spc)
{
if(!spc)
return ;
tr[spc].lzt^=;
std::swap(lll,rrr);
std::swap(tr[spc].lsum,tr[spc].rsum);
return ;
}
void pushdown(int spc)
{
if(tr[spc].lzt)
{
trr(lll);
trr(rrr);
tr[spc].lzt=;
}
if(tr[spc].add)
{
Add(lll,tr[spc].add);
Add(rrr,tr[spc].add);
tr[spc].add=;
}
return ;
}
void recal(int spc)
{
if(!tr[spc].anc)
recal(tr[spc].fa);
pushdown(spc);
return ;
}
void rotate(int spc)
{
int f=tr[spc].fa;
bool k=whc(spc);
tr[f].ch[k]=tr[spc].ch[!k];
tr[spc].ch[!k]=f;
if(tr[f].anc)
{
tr[spc].anc=true;
tr[f].anc=false;
}else
tr[tr[f].fa].ch[whc(f)]=spc;
tr[spc].fa=tr[f].fa;
tr[f].fa=spc;
tr[tr[f].ch[k]].fa=f;
pushup(f);
pushup(spc);
return ;
}
void splay(int spc)
{
recal(spc);
while(!tr[spc].anc)
{
int f=tr[spc].fa;
if(tr[f].anc)
{
rotate(spc);
return ;
}
if(whc(spc)^whc(f))
rotate(spc);
else
rotate(f);
rotate(spc);
}
return ;
}
void access(int spc)
{
int lst=;
while(spc)
{
splay(spc);
tr[rrr].anc=true;
tr[lst].anc=false;
rrr=lst;
pushup(spc);
lst=spc;
spc=tr[spc].fa;
}
return ;
}
void Mtr(int spc)
{
access(spc);
splay(spc);
trr(spc);
return ;
}
void split(int x,int y)
{
Mtr(x);
access(y);
splay(y);
return ;
}
bool together(int x,int y)
{
split(x,y);
while(tr[y].ls)
y=tr[y].ls;
return x==y;
}
void link(int x,int y)
{
split(x,y);
tr[x].fa=y;
return ;
}
void cut(int x,int y)
{
split(x,y);
tr[y].ls=;
tr[x].fa=;
tr[x].anc=true;
pushup(y);
return ;
}
lnt gcd(lnt a,lnt b)
{
if(!b)
return a;
return gcd(b,a%b);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%lld",&tr[i].val);
tr[i].anc=true;
pushup(i);
}
for(int i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
link(a,b);
}
while(m--)
{
int cmd;
scanf("%d",&cmd);
if(cmd==)
{
int u,v;
scanf("%d%d",&u,&v);
if(together(u,v))
cut(u,v);
}else if(cmd==)
{
int u,v;
scanf("%d%d",&u,&v);
if(!together(u,v))
link(u,v);
}else if(cmd==)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
if(together(u,v))
Add(v,d);
}else{
int u,v;
scanf("%d%d",&u,&v);
if(!together(u,v))
printf("%d\n",-);
else{
lnt top=tr[v].ans;
lnt bot=(lnt)(tr[v].wgt)*(lnt)(tr[v].wgt+)/;
lnt c=gcd(top,bot);
top/=c,bot/=c;
printf("%lld/%lld\n",top,bot);
}
}
}
return ;
}

BZOJ3091: 城市旅行(LCT,数学期望)的更多相关文章

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

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

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

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

  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

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

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

  6. 【LCT】BZOJ3091 城市旅行

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

  7. BZOJ 3091: 城市旅行 lct 期望 splay

    https://www.lydsy.com/JudgeOnline/problem.php?id=3091 https://blog.csdn.net/popoqqq/article/details/ ...

  8. 【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 题解 LCT区间合并 前三个 ...

  9. bzoj 3091: 城市旅行 LCT

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=3091 题解: 首先前三个操作就是裸的LCT模板 只考虑第四个操作. 要求我们计算期望,所以我 ...

随机推荐

  1. 理解FPGA中的RAM、ROM和CAM;ROM、RAM、DRAM、SRAM、FLASH

    目前大多数FPGA都有内嵌的块RAM(Block RAM),可以将其灵活地配置成单端口RAM(DPRAM,Single Port RAM).双端口RAM(DPRAM,Double Ports RAM) ...

  2. windows常用命令有哪些(整理)

    windows常用命令有哪些(整理) 一.总结 一句话总结:其实这个好学,只要先弄懂主干,清除主干,那么枝叶的添加逻辑就很清除了 这种多内容的,散乱的,弄清除主干效率就高了 1.windows命令行的 ...

  3. NetFlow是一种数据交换方式,提供网络流量的会话级视图,记录下每个TCP/IP事务的信息

    NetFlow是一种数据交换方式,提供网络流量的会话级视图,记录下每个TCP/IP事务的信息.也许它不能象tcpdump那样提供网络流量的完整记录,但是当汇集起来时,它更加易于管理和易读.Netflo ...

  4. 2017.9.17校内noip模拟赛解题报告

    预计分数:100+60+60=220 实际分数:100+60+40=200 除了暴力什么都不会的我..... T1 2017.9.17巧克力棒(chocolate) 巧克力棒(chocolate)Ti ...

  5. axure中使用HighCharts模板制作统计图表

    一. 步骤: 1.在axure中新建页面,发布并生成html文件: 2.将HighCharts文件夹,拷贝到生成的html文件中: 3.拖拽“内部框架组件”到界面中 4.双击界面中的内部框架,设置链接 ...

  6. MySQL Server 5.5.44免安装版配置详解

    转载地址:http://wenku.baidu.com/view/2a8bfe6a25c52cc58bd6beff.html### 一 下载MySQL http://dev.mysql.com/dow ...

  7. 第一次接触正则表达式/^[A-Za-z_][A-Za-z0-9_]{5,15}$/

    /^[A-Za-z_][A-Za-z0-9_]{5,15}$/ /^$/ :完整表达式 ^ :表示以什么开始,或者取反 $ :结束 ^[A-Za-z_] : 以字母开始,无论大小都可以: [^A-Za ...

  8. sql中 where语句的用法

    //查询user_id 10到20 之间  30到40之间 //查询user_id 不在10到20之间的

  9. ps切图时常用的操作与快捷键

    一:两种切片方法 第一种: 1.使用切片工具划分好你要切的模块 2.点击'存储为web所有格式',在存储之前可以修改图片的品质来改变文件的大小. 3.在存储时切片有三种选择方式,按照自己的需要选择. ...

  10. power design设计数据库

    power design是收费软件 大致设计流程: 画出概念数据模型,添加实体,连接实体间关系 生成物理数据模型,可以继续在此基础上修改 生成数据库脚本(一个.sql文件),文件中前面是删除表,后面是 ...