题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4530/

题解

想要求出一条边的负载那么就是要求出一个点为根的时候的另一个点的子树大小。

又因为要动态加边,所以只能用 LCT。

(突然想起来因为没有强制在线是不是可以把树先建起来然后跑以时间为权值的主席树?

但是 LCT 只能维护链上的信息,怎么维护子树大小呢?

所以就来搞一个维护子树信息的 LCT。

LCT 上每个点开一个变量 \(sum\) 表示这个点的所有轻儿子的子树大小之和,\(siz\) 表示这个点在 Splay 上的子树的权值和。

(很像 ddp 呢

然后 access 的时候,更换重儿子的时候,需要先断掉旧的重儿子,连上新的,所以需要先减去旧重儿子的权值,加上新重儿子的权值。


时间复杂度就是纯 LCT 吧,\(O(q\log n)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 100000 + 7; #define lc c[0]
#define rc c[1] int n, m; struct Node { int c[2], s, sum, siz, fa, rev; } t[N];
int st[N];
inline bool idtfy(int o) { return t[t[o].fa].rc == o; }
inline bool isroot(int o) { return t[t[o].fa].lc != o && t[t[o].fa].rc != o; }
inline void connect(int fa, int o, int d) { t[fa].c[d] = o, t[o].fa = fa; }
inline void pushup(int o) {
t[o].s = t[t[o].lc].s + t[t[o].rc].s + 1;
t[o].siz = t[t[o].lc].siz + t[t[o].rc].siz + t[o].sum + 1;
}
inline void pushdown(int o) {
if (!t[o].rev) return;
if (t[o].lc) t[t[o].lc].rev ^= 1, std::swap(t[t[o].lc].lc, t[t[o].lc].rc);
if (t[o].rc) t[t[o].rc].rev ^= 1, std::swap(t[t[o].rc].lc, t[t[o].rc].rc);
t[o].rev = 0;
}
inline void rotate(int o) {
int fa = t[o].fa, pa = t[fa].fa, d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
if (!isroot(fa)) t[pa].c[d2] = o; t[o].fa = pa;
connect(o, fa, d1 ^ 1), connect(fa, b, d1);
pushup(fa), pushup(o);
}
inline void splay(int o) {
int x = o, tp = 0;
st[++tp] = x;
while (!isroot(x)) st[++tp] = x = t[x].fa;
while (tp) pushdown(st[tp--]);
while (!isroot(o)) {
int fa = t[o].fa;
if (isroot(fa)) rotate(o);
else if (idtfy(o) == idtfy(fa)) rotate(fa), rotate(o);
else rotate(o), rotate(o);
}
}
inline void access(int o) {
for (int x = 0; o; o = t[x = o].fa) {
splay(o);
t[o].sum += t[t[o].rc].siz;
t[o].sum -= t[x].siz;
t[o].rc = x;
pushup(o);
}
}
inline void mkrt(int x) {
access(x), splay(x);
t[x].rev ^= 1, std::swap(t[x].lc, t[x].rc);
}
inline int getrt(int x) {
access(x), splay(x);
while (pushdown(x), t[x].lc) x = t[x].lc;
splay(x);
return x;
}
inline void link(int x, int y) {
mkrt(x);
if (getrt(y) != x) {
access(y), splay(y);
t[x].fa = y, t[y].sum += t[x].siz, pushup(y);
}
} inline void work() {
while (m--) {
char opt[5];
int x, y;
scanf("%s", opt);
read(x), read(y);
if (*opt == 'Q') mkrt(x), access(y), splay(x), assert(t[x].rc == y), assert(t[x].siz > t[y].siz), printf("%lld\n", (ll)t[y].siz * (t[x].siz - t[y].siz));
else link(x, y);
}
} inline void init() {
read(n), read(m);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj4530 [Bjoi2014]大融合 子树信息 LCT的更多相关文章

  1. [BZOJ4530][Bjoi2014]大融合 LCT + 启发式合并

    [BZOJ4530][Bjoi2014]大融合 试题描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是 ...

  2. BZOJ4530[Bjoi2014]大融合——LCT维护子树信息

    题目描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够 联通的树上路过它的简单路径的数 ...

  3. [bzoj4530][Bjoi2014]大融合_LCT

    大融合 bzoj-4530 Bjoi-2014 题目大意:n个点,m个操作,支持:两点连边:查询两点负载:负载.边(x,y)的负载就是将(x,y)这条边断掉后能和x联通的点的数量乘以能和y联通的点的数 ...

  4. [BZOJ4530][Bjoi2014]大融合(LCT)

    传送门 大佬们似乎都是用树剖+并查集优雅地A了此题 然后我太弱了,只能打打LCT的板子 虽然的确可以挺无脑的A掉…… 不过至少这题教了我该怎么维护LCT上虚子树的信息,具体看这里 首先,答案很明显是断 ...

  5. BZOJ4530:[BJOI2014]大融合(LCT)

    Description 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够 联通的树上路过它 ...

  6. BZOJ4530 BJOI2014大融合(线段树合并+并查集+dfs序)

    易知所求的是两棵子树大小的乘积.先建出最后所得到的树,求出dfs序和子树大小.之后考虑如何在动态加边过程中维护子树大小.这个可以用树剖比较简单的实现,但还有一种更快更优美的做法就是线段树合并.对每个点 ...

  7. 2019.01.14 bzoj4530: [Bjoi2014]大融合(线段树合并)

    传送门 线段树合并菜题. 题意简述:nnn个点,支持连边以及查询一个点所在连通块中经过这个点的路径条数,保证这张图时刻为森林. 思路: 先建出所有操作完之后的树统计出dfsdfsdfs序 注意有可能是 ...

  8. BZOJ4530: [Bjoi2014]大融合

    Description 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够 联通的树上路过它 ...

  9. P4219 [BJOI2014]大融合(LCT)

    P4219 [BJOI2014]大融合 对于每个询问$(u,v)$所求的是 ($u$的虚边子树大小+1)*($v$的虚边子树大小+1) 于是我们再开个$si[i]$数组表示$i$的虚边子树大小,维护一 ...

随机推荐

  1. VS2010护眼界面(护眼绿)

    工具-->选项 RGB(204.232.207) “选择菜单[工具][导入和导出设置], 在弹出的“导入和导出设置向导”对话框中选择“导入选定的环境设置”, 然后根据自己的实际情况选择“是,保存 ...

  2. k8s架构

    master节点 k8s的集群由master和node组成,节点上运行着若干k8s服务. master节点之上运行着的后台服务有kube-apiserver .kube-scheduler.kube- ...

  3. oracle data guard --理论知识回顾02

    继上一篇 管理影响物理standby的事件 1 创建表空间或数据文件初始化参数standby_file_management用来控制是否自动将primary数据库增加表空间或数据文件的改动,传播到st ...

  4. java.io.FileNotFoundException: /usr/local/hadoop/logs/fairscheduler-statedump.log(权限不够)解决方案

    问题描述:Linux虚拟机内使用hadoop 解决方案: $ su 密码:****** # chown hadoop 文件名 进入超级管理员,为hadoop用户分配该文件的权限. 完美解决:

  5. JavaScript —— 实现简单计算器【带有 开/关机 清零 退格 功能】

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. GCC 环境变量 & eclipse CDT 头文件配置

     转:http://blog.csdn.net/statdm/article/details/7751000 GCC 环境变量 & eclipse CDT 头文件配置   在unix 下使用e ...

  7. switch条件变量的取值类型

    switch条件变量的取值类型主要有以下六种: 1)JDK1.5(不含JDK1.5)之前只能是byte.short.int.char类型,不能是float.double.long.boolean类型. ...

  8. Mybatis-学习笔记(5)动态SQL

    1.Mybatis采用功能强大的基于ONGL的表达式来完成动态SQL. 2.ONGL常用的元素有: 1>if <if test="id != null "> an ...

  9. 2016青岛区域赛.Coding Contest(费用流 + 概率计算转换为加法计算)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  10. numpy库的认识以及数组的创建

    numpy库 numpy是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础.numpy十分高效,基于NumPy的算法要比纯Python快10到100倍(甚至 ...