CF1110F Nearest Leaf】的更多相关文章

传送门 这是我第二次看见这个题目了,第一次看见是另一场比赛的A题,想了一个小时不会写就弃了 从来没想过这个题能这么玩 线段树上记录根到叶子节点的距离 初始线段树上先记下根节点1到各叶子节点的距离 先离线,然后dfs整颗树,在dfs过程中考虑根的移动对线段树的影响 如果当前点是查询点,在当前线段树上查询 代码: #include<cstdio> #include<iostream> #include<algorithm> using namespace std; void…
Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of a tree (a connected undirected graph without cycles) as follows: consider a depth-first search algorithm which traverses vertices of the tree and enu…
题目链接 \(dls\)讲过这道题,所以这不是线段树裸题吗,这场没打气气气气气=-= 现在是写着玩=v= \(Description\) 给定一棵\(n\)个点的树.\(q\)次询问,每次询问给定\(v,l,r\),求离\(v\)最近且DFS序在\([l,r]\)之间的叶节点是哪个. \(n,q\leq5\times10^5\). \(Solution\) 把询问离线. 在树上走,每次从\(fa[x]\)走到\(x\)时,设边权为\(len\).此时距离\(x\)子树外的点的距离会增加\(len…
我--又诈尸了-- 代码几乎都不会写了,打场CF居然上分啦,开心!(虽然还是比不过列表里的各路神仙) 题目链接 题目描述 一棵\(n\)个点的有根树,规定一种dfs序(规则:编号小的点优先dfs),\(m\)次询问一个点\(u\)和一个区间\([l, r]\),求dfs序在这个区间内的叶子中,到\(u\)最小的距离. \(n, m \le 500000\) 题解 这题--很简单-- 题面一上来给个什么欧拉遍历定义--我吓得比赛中没看这题--(实际上码量对于代码几乎都不会敲的退役选手来说,不是非常…
题目传送门 题解: 先用题目给定的dfs方式得到dfs序,记录下出入的dfs序. 很明显可以得知的是,以u为根的子树的dfs序在 in[u] - out[u] 的范围之内. 将每个询问先全部存到对应的节点上. 然后我们以1为root,先求出每个叶子节点到1的距离. 对1的询问查询更新完答案之后. (假设1和2之间有一条权值为w的边) 那么以2为根的话,就相当于是 in[2] --- out[2] 区间里面的所有值的距离 -= w, 其他点的距离 += w. 然后对于每个点都这样处理. 注意修改和…
Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of th…
[抄题]: Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf…
Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of th…
原题链接在这里:https://leetcode.com/problems/closest-leaf-in-a-binary-tree/ 题目: Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. Here, nearest to a leaf means the l…
主要是记录思路,不要被刚开始错误方向带偏了 www 「CF1110F」Nearest Leaf 特殊性质:先序遍历即为 \(1 \to n\),可得出:叶子节点编号递增或可在不改变树形态的基础上调整为递增. 这样就可找出区间 \([l, r]\) 中的叶子节点有哪些了,预处理深度,暴力 \(O(n ^ 2)\). 考虑柿子 \(\min \{\mathrm{d} (y) - \mathrm{d} (\mathrm{f} (x))\}\),其中 \(d(x)\) 表示深度,\(f(x)\) 表示父…