BZOJ3091: 城市旅行(LCT,数学期望)
Description
.jpg)
Input
.jpg)
Output
.jpg)
Sample Input
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
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,数学期望)的更多相关文章
- BZOJ 3091: 城市旅行 [LCT splay 期望]
3091: 城市旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1454 Solved: 483[Submit][Status][Discuss ...
- bzoj3091 城市旅行 LCT + 区间合并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3091 题解 调了整个晚自习才调出来的问题. 乍一看是个 LCT 板子题. 再看一眼还是个 LC ...
- 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& ...
- BZOJ3091 城市旅行 LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3091 题意概括 鉴于本人语文不好,此题的描述原题很清晰,废话不多,请看原题. 可怕,原题是图片,不 ...
- 【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 ...
- 【LCT】BZOJ3091 城市旅行
3091: 城市旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1927 Solved: 631[Submit][Status][Discuss ...
- BZOJ 3091: 城市旅行 lct 期望 splay
https://www.lydsy.com/JudgeOnline/problem.php?id=3091 https://blog.csdn.net/popoqqq/article/details/ ...
- 【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区间合并 前三个 ...
- bzoj 3091: 城市旅行 LCT
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=3091 题解: 首先前三个操作就是裸的LCT模板 只考虑第四个操作. 要求我们计算期望,所以我 ...
随机推荐
- uvalive 4730王国kingdom(并查集+线段树)
题意:有T组測试数据.每组数据的N表示有N个城市,接下来的N行里每行给出每一个城市的坐标(0<=x,y<=1000000),然后有M(1<M<200000)个操作,操作有 ...
- QThread 爬坑之旅(三种办法解决QObject: Cannot create children for a parent that is in a different thread)
Cannot create children for a parent that is in a different thread. 在Qt的官方文档,大家知道有两种方式使用QThread. You ...
- Android-Universal-Image-Loader 学习笔记(五)线程池分析
UniveralImageLoader中的线程池 一般情况网络访问就需要App创建一个线程来执行(不然可能出现很臭的ANR),但是这也导致了当网络访问比较多的情况下,线程的数目可 ...
- 如何判断自己IP是内网IP还是外网IP
tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255 172.16.0.0/12:172.16.0.0- ...
- 80.简单搭建nodeJS服务,访问本地站点文件
转自:https://blog.csdn.net/iteye_1217/article/details/82679843 搭建nodejs服务器步骤: 1.安装nodejs服务(从官网下载安装),no ...
- C++中的namespace详解
原文链接:http://blog.csdn.net/yao_zhuang/article/details/1853625 namespace中文意思是命名空间或者叫名字空间,传统的C++只有一个全局的 ...
- 大吉大利,晚饭吃鸡!——accept关闭问题
假期收尾了,学芽子们都军训了.一群张一山和周冬雨在校内晃晃悠悠,说起来春风十里也就军训比较有意思.对于我这种一年追一部剧的人,显然是有点对不住.在我假期任务即将圆满之际,我开始放慢脚步寻找生活的美妙时 ...
- UDP连接调用connect()函数
UDP是一个无连接的协议,它没有像TCP中EOF之类的东西. 8.11 UDP的connect函数 除非套接字已连接,否则异步错误是不会反悔到UDP套接字的. 我们确实能够给UDP套接字调用conne ...
- HDOJ 5414 CRB and String 模拟
CRB and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- jquery11源码 animate() : 运动的方法
{ var fxNow, timerId, rfxtypes = /^(?:toggle|show|hide)$/, rfxnum = new RegExp( "^(?:([+-])=|)( ...