ABC294G Distance Queries on a Tree 题解

ABC294G

即给定一棵树,要求支持子树加和查询 $u, v$ 间的路径。

可以维护 $\texttt{dfs}$ 序,这样就变成了序列上的区间加。

每次求出 LCA 后,查询 $d_{dfn_u}, d_{dfn_v}, d_{dfn_{lca}}$ 即可,$d$ 是当前 $\texttt{dfs}$ 序所表示的点的深度,使用线段树维护即可。

于是只需要线段树和求 LCA 即可,我用的倍增 LCA。

时间复杂度:$\mathcal{O}({n\log n+q\log n)}$

评测记录

鲜花:应该只有我写了 3k,想知道怎么做到 $60$ 行还不压的。

posted @ 2023-03-20 22:27  Pengzt  阅读(5)  评论(0编辑  收藏  举报  来源