[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 <=…
题意 给出一棵以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 即…
题目传送门 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(…
子树操作, 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$…
题目描述 给出以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…
BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序 题意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最小的串,并输出这些串.n <= 30,000 , m <= 300,000 分析: 首先不考虑大小关系,如果一个串是另一个串的前缀,那么另一个串一定不能成为字典序最小的串,我们可以用trie树很好的解决. 考虑前缀相同的情况,这个串在前缀后的字符应该和含有相同前缀的串在前缀后的字符有明确的大小关系…
[BZOJ3012][Usaco2012 Dec]First! Description Bessie has been playing with strings again. She found that by changing the order of the alphabet she could make some strings come before all the others lexicographically (dictionary ordering). For instance…
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…