题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3011 题解 复习一下左偏树板子. 看完题目就知道是左偏树了. 结果这个板子还调了好久. 大概已经不会写左偏树了. 时间复杂度 \(O(n\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(…
题意 给出一棵以1为根节点树,求每个节点的子树中到该节点距离<=l的节点的个数 题解 方法1:倍增+差分数组 首先可以很容易的转化问题,考虑每个节点对哪些节点有贡献 即每次对于一个节点,找到其第l个父亲,这个操作可以用倍增在logn时间内完成 找到后将x-y这一段区间都加1,很容易想到用差分数组维护 方法2:主席树 考虑节点x和节点x的子树中的一个节点y,记点x到根节点的距离为dis[x] 若dis[y]-dis[x]<=l则满足条件 将不等式变形可得dis[y]<=dis[x]+l 即…
[BZOJ3011][Usaco2012 Dec]Running Away From the Barn Description It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to round them all up, and needs your help in the search. FJ's farm is a series of N (1 <= N <=…
子树操作, dfs序即可.然后计算<=L就直接在可持久化线段树上查询 ------------------------------------------------------------------- #include<bits/stdc++.h>   using namespace std;   #define M(l, r) (((l) + (r)) >> 1)   const int maxn = 200009;   typedef long long ll;  …
BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆 Description 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于l的点有多少个. Sample Input 4 5 1 4 2 3 1 5 Sample Output 3 2 1 1 做法不唯一,这里用来练习可并堆. 先求出每个点$i$ 到根路径上的长度$dis[i]$ ,对每个点建一个可并堆(大根). 然后从下往上合并,如果当前$dis[堆顶]-dis[x]>L$…
P3066 逃跑的Barn 左偏树 题面 题意:给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 注意到答案的两个性质: 一个点的所有答案一定包含在其所有儿子的答案中 如果节点\(i​\)当前满足条件,那么所有距离(相对于根节点)比它小的节点当前也都满足(所以建个大根堆) 所以考虑使用左偏树,每个节点都建个大根堆,在\(dfs\)时计算出所有点深度,再利用这些性质回溯时依次合并所有堆,显然答案即为堆的大小. 注意long long卡了我好久 #include <cs…
题目传送门 逃跑的Barn 题目描述 It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to round them all up, and needs your help in the search. FJ's farm is a series of N (1 <= N <= 200,000) pastures numbered 1...N connected b…
题目描述 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 输入格式 Line 1: 2 integers, N and L (1 <= N <= 200,000, 1 <= L <= 10^18) Lines 2..N: The ith line contains two integers p_i and l_i. p_i (1 <= p_i < i) is the first pasture on the shortest path b…
题目大意: 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于m的点有多少个 左偏树 https://blog.csdn.net/pengwill97/article/details/82874235 题解https://www.cnblogs.com/GXZlegend/p/6532881.html 若y在x的子树内 那么x到y的距离 等于 dis(1,y)-dis(1,x) 所以DFS时处理出节点到根(点1)的距离 然后自底向上合并 维护距离大顶堆 那么当 堆顶到根的距离 > m…
Description It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to round them all up, and needs your help in the search. FJ's farm is a series of N (1 <= N <= 200,000) pastures numbered 1...N connected by N - 1…