BZOJ3091 城市旅行 LCT
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ3091
题意概括
鉴于本人语文不好,此题的描述原题很清晰,废话不多,请看原题。
可怕,原题是图片,不可以复制题目+删掉废话了……
题解
http://blog.csdn.net/popoqqq/article/details/40823659
这位大佬写的很好。
我的代码在找错的时候一边找,一边该,然后发现和他改的好像……
代码
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long LL;
const int N=50005;
struct Gragh{
int cnt,y[N*2],nxt[N*2],fst[N];
void clear(){
cnt=0;
memset(fst,0,sizeof fst);
}
void add(int a,int b){
y[++cnt]=b,nxt[cnt]=fst[a],fst[a]=cnt;
}
}g;
int n,m;
int fa[N],son[N][2],rev[N];
LL size[N],val[N],add[N],sum[N],Lsum[N],Rsum[N],Exp[N];
bool isroot(int x){
return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}
void pushup(int x){
int ls=son[x][0],rs=son[x][1],lsz=size[ls],rsz=size[rs];
size[x]=lsz+rsz+1;
sum[x]=sum[ls]+sum[rs]+val[x];
Lsum[x]=Lsum[ls]+(lsz+1)*val[x]+Lsum[rs]+sum[rs]*(lsz+1);
Rsum[x]=Rsum[rs]+(rsz+1)*val[x]+Rsum[ls]+sum[ls]*(rsz+1);
Exp[x]=Exp[ls]+Exp[rs]+Lsum[ls]*(rsz+1)+Rsum[rs]*(lsz+1)+val[x]*(lsz+1)*(rsz+1);
}
void pushson(int x,LL v){
if (!x)
return;
val[x]+=v,sum[x]+=v*size[x],add[x]+=v;
Lsum[x]+=v*size[x]*(size[x]+1)/2;
Rsum[x]+=v*size[x]*(size[x]+1)/2;
Exp[x]+=v*size[x]*(size[x]+1)*(size[x]+2)/6;
}
void pushrev(int x){
rev[x]^=1;
swap(son[x][0],son[x][1]);
swap(Lsum[x],Rsum[x]);
}
void pushdown(int x){
int &ls=son[x][0],&rs=son[x][1];
if (rev[x]){
rev[x]=0;
pushrev(ls);
pushrev(rs);
}
if (add[x]){
LL &a=add[x];
pushson(ls,a);
pushson(rs,a);
a=0;
}
}
void pushadd(int x){
if (!isroot(x))
pushadd(fa[x]);
pushdown(x);
}
int wson(int x){
return son[fa[x]][1]==x;
}
void rotate(int x){
if (isroot(x))
return;
int y=fa[x],z=fa[y],L=wson(x),R=L^1;
if (!isroot(y))
son[z][wson(y)]=x;
fa[x]=z,fa[y]=x,fa[son[x][R]]=y;
son[y][L]=son[x][R],son[x][R]=y;
pushup(y),pushup(x);
}
void splay(int x){
pushadd(x);
for (int y=fa[x];!isroot(x);rotate(x),y=fa[x])
if (!isroot(y))
rotate(wson(x)==wson(y)?y:x);
}
void access(int x){
int t=0;
while (x){
splay(x);
son[x][1]=t;
pushup(x);
t=x;
x=fa[x];
}
}
void rever(int x){
access(x);
splay(x);
pushrev(x);
}
void link(int x,int y){
rever(x);
fa[x]=y;
}
void cut(int x,int y){
rever(x);
access(y);
splay(y);
fa[x]=son[y][0]=0;
}
int find(int x){
access(x);
splay(x);
while (1){
pushdown(x);
if (son[x][0])
x=son[x][0];
else
break;
}
return x;
}
void dfs(int x,int pre){
fa[x]=pre;
for (int i=g.fst[x];i;i=g.nxt[i])
if (g.y[i]!=pre)
dfs(g.y[i],x);
}
LL gcd(LL a,LL b){
return b==0?a:gcd(b,a%b);
}
void solve(int x,int y){
if (find(x)!=find(y)){
puts("-1");
return;
}
rever(x);
access(y);
splay(y);
LL a=Exp[y];
LL b=size[y]*(size[y]+1)/2;
LL Gcd=gcd(a,b);
a/=Gcd,b/=Gcd;
printf("%lld/%lld\n",a,b);
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++){
scanf("%lld",&val[i]);
fa[i]=son[i][0]=son[i][1]=rev[i]=0;
size[i]=1,Exp[i]=Lsum[i]=Rsum[i]=sum[i]=val[i],add[i]=0;
}
g.clear();
for (int i=1,a,b;i<n;i++){
scanf("%d%d",&a,&b);
g.add(a,b),g.add(b,a);
}
dfs(1,0);
for (int i=1;i<=m;i++){
int op,x,y;
LL v;
scanf("%d%d%d",&op,&x,&y);
if (op==1){
if (find(x)==find(y))
cut(x,y);
}
else if (op==2){
if (find(x)!=find(y))
link(x,y);
}
else if (op==3){
scanf("%lld",&v);
if (find(x)!=find(y))
continue;
rever(x);
access(y);
splay(y);
pushson(y,v);
}
else
solve(x,y);
}
return 0;
}
BZOJ3091 城市旅行 LCT的更多相关文章
- 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 + 区间合并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3091 题解 调了整个晚自习才调出来的问题. 乍一看是个 LCT 板子题. 再看一眼还是个 LC ...
- 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 ...
- 【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 期望]
3091: 城市旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1454 Solved: 483[Submit][Status][Discuss ...
- 【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 期望 splay
https://www.lydsy.com/JudgeOnline/problem.php?id=3091 https://blog.csdn.net/popoqqq/article/details/ ...
- bzoj 3091: 城市旅行 LCT
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=3091 题解: 首先前三个操作就是裸的LCT模板 只考虑第四个操作. 要求我们计算期望,所以我 ...
随机推荐
- buildroot构建项目(七)--- u-boot 2017.11 适配开发板修改 4 ---- 系统启动初始化之四
设置完寄存器控制器后,则跳出cpu_init_crit,进入_main 函数.即进入crt0.S (arch\arm\lib) 跟着代码流程慢慢走 一.crt0.S 1.1 第一步执行代码 /* 预 ...
- HttpWebRequest post 提交 C#的WebBrowser操作frame如此简单 WebClient 提交
//http://www.cnblogs.com/cgli/archive/2011/04/09/2010497.html System.Net.ServicePointManager.Expect1 ...
- 【SVN】svn使用方法
下载安装TortoiseSVN 下载地方 安装成功后 TortoiseSVN清除凭证 右击空白处-TortoiseSVN-Settings打开Settings窗口后做如下操作: svn在idea中的使 ...
- js sort方法根据数组中对象的某一个属性值进行排序
sort方法接收一个函数作为参数,这里嵌套一层函数用来接收对象属性名,其他部分代码与正常使用sort方法相同. var arr = [ {name:'zopp',age:0}, {name:'gpp' ...
- 【Udacity并行计算课程笔记】- Lesson 3 Fundamental GPU Algorithms (Reduce, Scan, Histogram)
本周主要内容如下: 如何分析GPU算法的速度和效率 3个新的基本算法:归约.扫描和直方图(Reduce.Scan.Histogram) 一.评估标准 首先介绍用于评估GPU计算的两个标准: ste ...
- 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法
在计算loss的时候,最常见的一句话就是 tf.nn.softmax_cross_entropy_with_logits ,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化 ...
- android okhttp的使用
OkHttpClient client = new OkHttpClient(); String url = ""; Request request = new Request.B ...
- kali Linux下wifi密码安全测试(1)虚拟机下usb无线网卡的挂载 【转】
转自:http://blog.chinaunix.net/uid-26349264-id-4455634.html 目录 kali Linux下wifi密码安全测试(1)虚拟机下usb无线网卡的挂载 ...
- python调用win32com.client的GetObject查找进程信息及服务信息
为何不用wmi呢?因为执行很慢,为啥不用winreg?因为winreg在批量获取及遍历服务方面很不方便,于是采用这方法 该方法同命令行下的wmic执行 获取服务信息 #coding=utf8 from ...
- wap页面缩放
html{font-size: 100%;}.in-main{ min-width:320px; max-width:640px; margin:0 auto; font-size:14px; bac ...