题目传送门

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. 复选框checked 选中后不显示打钩

    复选框checked 选中后不显示打钩 checkbox属性checked="checked"已有,但复选框却不显示打钩的原因   复选框绑定了click事件,点一次选中,再点击取 ...

  2. Python的datetime与Decimal数据进行json序列化的简单说明

    我们在Python的json.JSONEncoder类中可以查看Python数据序列化为JSON格式的数据时数据类型的对应关系: class JSONEncoder(object): "&q ...

  3. cmake使用2

    CMake支持大写.小写.混合大小写的命令. . 添加头文件目录INCLUDE_DIRECTORIES 语法:include_directories([AFTER|BEFORE] [SYSTEM] d ...

  4. 大sd卡 裂开了,写保护掉了。重新装好后,被写保护的解决办:

    大sd卡 裂开了,写保护掉了.重新装好后,被写保护的解决办: 1.用烙铁把写保护附近的塑料往外顶一点点,就ok   别太热,也别劲太大.容易过,不能破坏原来的部分. 解决问题. 总结: 写保护,就是检 ...

  5. lnmp 安装yarn

    Linux 安装 curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/y ...

  6. CMakeLists.txt中使用循环

    编译一个安卓下的so,此so依赖其他的库,通过循环简化操作 set(UVC_LIBS UVCCamera uvc usb100 jpeg-turbo1500) FOREACH(UVC_LIB ${UV ...

  7. postgresql 10.5 主从复制--搭建测试

    env: role master slave host pg1 pg2 ip 11 12 pg-version 10.5 10.5 1 初始化查看 [ceiec@localhost ~]$ df -h ...

  8. big data env setup

    install Spark on CentOS: https://aodba.com/how-to-install-apache-spark-in-centos-standalone/ https:/ ...

  9. 个人推荐的两款vue导出EXCEL插件

    个人认为前端VUE项目中导出EXCEL比较好的两种方法,均不是我个人原创,我只是收录简单说明,原创地址在下面. 下面推荐两种方法,个人推荐第一种,第二种不做详细讲解,因为作者已经写过博客了,你们可以点 ...

  10. 通过proxychains实现Ubuntu终端代理

    1.在终端内使用代理,需要使用proxychains: sudo apt-get install proxychains 2.编辑 /etc/proxychains.conf sudo gedit / ...