BZOJ 3091: 城市旅行 lct 期望 splay
https://www.lydsy.com/JudgeOnline/problem.php?id=3091
https://blog.csdn.net/popoqqq/article/details/40823659
看题解吧,没什么好解释的。。。。板子题,
我觉得以后我写lct都可以像这样专门写个rev和add的函数出来,很好用。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=;
int n,m;
int fa[maxn]={},ch[maxn][]={};
long long siz[maxn]={},val[maxn]={},sum[maxn]={};
long long lsum[maxn]={},rsum[maxn]={};
long long num[maxn]={};long long ad[maxn]={};
int rev[maxn]={};int sta[maxn]={},tail=;
inline void updata(int x){
sum[x]=sum[ch[x][]]+sum[ch[x][]]+val[x];
siz[x]=siz[ch[x][]]+siz[ch[x][]]+; lsum[x]=lsum[ch[x][]]+lsum[ch[x][]]+(sum[ch[x][]]+val[x])*(siz[ch[x][]]+);
rsum[x]=rsum[ch[x][]]+rsum[ch[x][]]+(sum[ch[x][]]+val[x])*(siz[ch[x][]]+); num[x]=num[ch[x][]]+num[ch[x][]]
+lsum[ch[x][]]*(siz[ch[x][]]+)
+rsum[ch[x][]]*(siz[ch[x][]]+)
+val[x]*(siz[ch[x][]]+)*(siz[ch[x][]]+);
}
inline long long hex(long long x){return x*(x+)/;}
inline long long hex2(long long x){return x*(x+)*(x+)/;}
void Add(int x,long long v){
ad[x]+=v; sum[x]+=siz[x]*v; val[x]+=v;
lsum[x]+=hex(siz[x])*v;
rsum[x]+=hex(siz[x])*v;
num[x]+=hex2(siz[x])*v;
}
void Rev(int x){
swap(ch[x][],ch[x][]);
swap(lsum[x],rsum[x]);
rev[x]^=;
}
inline void downdata(int x){
if(rev[x]){
if(ch[x][])Rev(ch[x][]);
if(ch[x][])Rev(ch[x][]);
updata(x);
rev[x]=;
}
if(ad[x]){
if(ch[x][])Add(ch[x][],ad[x]);
if(ch[x][])Add(ch[x][],ad[x]);
updata(x);
ad[x]=;
}
}
inline bool isroot(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
void rotate(int x){
int y=fa[x];int fy=fa[y];
int l=ch[y][]==x?:;int r=l^;
if(!isroot(y)){
if(ch[fy][]==y)ch[fy][]=x;
else ch[fy][]=x;
}
fa[ch[x][r]]=y;fa[y]=x;fa[x]=fy;
ch[y][l]=ch[x][r];ch[x][r]=y;
updata(y);
}
void Splay(int x){
int y,fy,w=x;sta[++tail]=x;
while(!isroot(w)){ sta[++tail]=fa[w]; w=fa[w]; }
while(tail)downdata(sta[tail--]);
while(!isroot(x)){
y=fa[x];fy=fa[y];
if(!isroot(y)){
if((ch[fy][]==y)^(ch[y][]==x)) rotate(x);
else rotate(y);
}
rotate(x);
}updata(x);
}
void Access(int x){
int y=;
while(x){
Splay(x);ch[x][]=y;
updata(x);
y=x;x=fa[y];
}
}
int findfa(int x){
Access(x);Splay(x);
while(ch[x][])x=ch[x][];
return x;
}
void Reverse(int x){
Access(x);Splay(x);
Rev(x);
}
void Cut(int x,int y){
Reverse(x);Access(y);Splay(y);
if(ch[y][]==x&&!ch[x][]){fa[x]=;ch[y][]=;updata(y);}
}
void Link(int x,int y){
Reverse(x);
fa[x]=y;
}
long long mgcd(long long x,long long y){
long long z;
while(y){
z=x%y;x=y;y=z;
}return x;
}
int main(){
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){scanf("%lld",&val[i]);siz[i]=;sum[i]=lsum[i]=rsum[i]=num[i]=val[i];}
int x,y,op;long long v,aa,bb;
for(int i=;i<n;i++){
scanf("%d%d",&x,&y); if(findfa(x)!=findfa(y))Link(x,y);
}
for(int i=;i<=m;i++){
scanf("%d%d%d",&op,&x,&y);
if(op==){
if(findfa(x)==findfa(y))Cut(x,y);
}
else if(op==){
if(findfa(x)!=findfa(y))Link(x,y);
}
else if(op==){
scanf("%lld",&v);
if(findfa(x)==findfa(y)){
Reverse(x);Access(y);Splay(y);
Add(y,v);
}
}
else{
if(findfa(x)!=findfa(y)){printf("-1\n");}
else{
Reverse(x);Access(y);Splay(y);
aa=num[y];bb=hex(siz[y]);
v=mgcd(aa,bb);
printf("%lld/%lld\n",aa/v,bb/v);
}
}
}
}
BZOJ 3091: 城市旅行 lct 期望 splay的更多相关文章
- BZOJ 3091: 城市旅行 [LCT splay 期望]
3091: 城市旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1454 Solved: 483[Submit][Status][Discuss ...
- bzoj 3091: 城市旅行 LCT
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=3091 题解: 首先前三个操作就是裸的LCT模板 只考虑第四个操作. 要求我们计算期望,所以我 ...
- bzoj 3091 城市旅行(LCT+数学分析)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3091 [思路] 膜Popoqqq大爷的题解 click here [代码]是坑... ...
- BZOJ 3091 城市旅行
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 ...
- 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 ...
- 【BZOJ】3091: 城市旅行 Link-Cut Tree
[题意]参考PoPoQQQ. 给定一棵树,每个点有一个点权,提供四种操作: 1.删除两点之间的连边 不存在边则无视 2.在两点之前连接一条边 两点已经联通则无视 3.在两点之间的路径上所有点的点权加上 ...
- 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& ...
随机推荐
- 2016.07.15——istringstream测试
istringstream测试 1.istringstream strcin(str),字符串(str)可以包括多个单词,单词之间使用空格分开 #include "stdafx.h" ...
- 高级C#信使(译) - Unity维基百科
高级C#信使 作者:Ilya Suzdalnitski 译自:http://wiki.unity3d.com/index.php/Advanced_CSharp_Messenger 描述 前言 Mis ...
- scp加端口号
scp -P 21110 root@192.168.0.1:/home/abc.txt root@192.168.0.2:/root 注意: 参数-P 的位置一定要紧跟在scp命令后面 参数-P 指的 ...
- ip分片重组 ip_defrag
在ip_local_deliver中,如果检测到是分片包,则需要进行分片重组: ip_local_deliver |-->ip_is_fragment //判断是否为分片包 |-->ip_ ...
- 对于Linux平台下C语言开发中__sync_函数的认识(转)
reference:http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html#Atomic-Builtins A built-i ...
- C# 各种类型的转换
/// <summary> /// 一些常用的方法 /// 1.一些高效的转换方法 /// </summary> public class Util { #region Obj ...
- git summary
Git 使用经验 缘起 一直想写一篇博文,记录我在使用git时遇到的问题以及解决办法.由于项目忙,偶尔的记录不连续,不成系统.今天有时间记录下来,方便自己以后查看. git 简介及其优势 简单来说,g ...
- APUE-文件和目录(八)文件时间
文件的时间 与文件相关的三个时间值: 访问时间:最后一次访问文件的时间.例如,cat命令会修改这个时间. 修改时间:文件内容最后一次被修改的时间. 状态更改时间:文件的i节点最后一次被修改的时间.例如 ...
- VirtualBox上安装CentOS-7(Minimal)
Windows 10家庭中文版,VirtualBox 5.2.12,CentOS 7(Minimal版), 因为听到大家在谈论CentOS,阿里云上也有CentOS,CentOS还是Red Hat出品 ...
- python网络编程-进程锁
一:进程锁的作用 进程锁是防止多进程并发执行在屏幕打印的时候,其他进程也输出数据到屏幕,而出现混乱现象. 比如:进程池中很多进程会向同一个日志文件中打印日志 二:代码 # -*- coding:utf ...