Luogu 3066 [USACO12DEC]逃跑的BarnRunning Away From…
好像是某CF的题,不记得……
很套路的题,但是觉得可以做一下笔记。
倍增 + 差分。
有一个比较简单的思路就是每一个点$x$向上走一走,直到走到一个点$y$使总路程恰好不超过超过了$L$,然后把$(x, y)$这条链上的答案$ + 1$。
可以用倍增优化走一走的过程,可以用差分实现把一条向上的树链$+ 1$的操作。
时间复杂度$O(nlogn)$。
Code:
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 2e5 + ;
const int Lg = ; int n, tot = , head[N], fa[N][Lg], f[N];
ll cur, dis[N][Lg]; struct Edge {
int to, nxt;
ll val;
} e[N << ]; inline void add(int from, int to, ll val) {
e[++tot].to = to;
e[tot].val = val;
e[tot].nxt = head[from];
head[from] = tot;
} template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} void dfs(int x, int fat, ll nowDis) {
fa[x][] = fat, dis[x][] = nowDis;
for(int i = ; i <= ; i++) {
fa[x][i] = fa[fa[x][i - ]][i - ];
dis[x][i] = dis[x][i - ] + dis[fa[x][i - ]][i - ];
}
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs(y, x, e[i].val);
}
} inline int getPos(int x) {
ll nowDis = 0LL;
for(int i = ; i >= ; i--)
if(nowDis + dis[x][i] <= cur) {
nowDis += dis[x][i];
x = fa[x][i];
}
return x;
} void solve(int x, int fat) {
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
solve(y, x);
f[x] += f[y];
}
} int main() {
read(n), read(cur);
for(int i = ; i <= n; i++) {
int fat; ll v;
read(fat), read(v);
add(fat, i, v), add(i, fat, v);
}
dfs(, , 0LL); /* for(int i = 1; i <= n; i++)
printf("%d ", dis[i][18]); */ for(int i = ; i <= n; i++) {
++f[i];
int pos = getPos(i);
--f[fa[pos][]];
} solve(, ); for(int i = ; i <= n; i++)
printf("%d\n", f[i]); return ;
}
Luogu 3066 [USACO12DEC]逃跑的BarnRunning Away From…的更多相关文章
- [USACO12DEC] 逃跑的BarnRunning Away From…(主席树)
[USACO12DEC]逃跑的BarnRunning Away From- 题目描述 It's milking time at Farmer John's farm, but the cows hav ...
- luoguP3066 [USACO12DEC]逃跑的BarnRunning
luoguP3066 [USACO12DEC]逃跑的BarnRunning 题目大意 给定一棵n个节点的树和参数L,查询每个节点子树中到达该节点距离<=L的数量(包括该节点) 偏模板的主席树 P ...
- [Luogu3066][USACO12DEC]逃跑的BarnRunning Away From…
题面 题目描述 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 输入格式: Line 1: 2 integers, N and L (1 <= N <= 2 ...
- 洛谷P3066 [USACO12DEC]逃跑的BarnRunning Away From…
题面链接 一句话题意:给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 我:似乎并不好做啊...看了题解后大雾... sol:考虑树上差分,对于一个点,在他那个位置++, ...
- [USACO12DEC]逃跑的BarnRunning Away From…
题意 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个 题解 似乎有好多种做法啊……然而蒟蒻只会打打主席树的板子…… 调了一个上午一直WA……狠下心来重打一遍居然直接一遍过 ...
- P3066 [USACO12DEC]逃跑的BarnRunning Away From
目录 题目 思路 错误&&注意 代码 题目 luoguP3066 思路 虽说这个题目有多种做法,但 左偏树算法: 我们发现这个合并的时候并不好合并,因为存的值不是固定的 那我们是不是可 ...
- P3066 [USACO12DEC]逃跑的BarnRunning Away From (树上二分)
题意 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 树上二分.这个做法还是基于树上差分的,也就是对于每一个点uu,我们要找到它向上跳LL的长度最高能够跳到的祖先.(当 ...
- P3066 [USACO12DEC]逃跑的BarnRunning Away From… 树上差分_树上倍增
code: #include <cstdio> using namespace std; #define ll long long const int N=200005; int n,fa ...
- [luogu P3065] [USACO12DEC]第一!First!
[luogu P3065] [USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that ...
随机推荐
- LeetCode Split Array into Consecutive Subsequences
原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...
- phpredis的使用
phpredis的具体使用方法可以参照:https://github.com/phpredis/phpredis
- Markdown编辑器推荐与语法教程--图片版
请参考Markdown编辑器推荐与语法教程--展示版或者Markdown编辑器推荐与语法教程--展示版看具体效果,当然,大家也可以下载Mou亲自体验一把 End
- ArcGIS_Desktop安装步骤
1.双击镜像文件 2.下一步 3.关闭 4.下一步 5.下一步 6.下一步 7.下一步 8.下一步 9.安装 10.一直到下一步安装完成
- 戴尔PowerEdge RAID控制卡使用示例(PERC H710P为例)
Dell PERC使用示例列表(H710p) 特别说明,本文相关RAID的操作,仅供网友在测试环境里学习和理解戴尔PowerEdge服务器RAID控制卡的功能和使用方法.切勿直接在生产服务器上做相关实 ...
- selenium - css 定位
前言: CSS(Cascading Style Sheets)是一种语言,它被用来描述 HTML 和 XML 文档的表现. CSS 使用选择器来为页面元素绑定属性.这些选择器可以被 selenium ...
- Socket代码
服务器端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...
- git 本地文件里内容 操作记录
本地环境文件合并分支(以下的都分别 commit提交了的) [一.分支[追加] 和 [新增] 新信息 合并主线 情景] 分支内容: dr.find_element_by_id("su&qu ...
- 1147 Heaps
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- HBuilder使用技巧
ctrl+ Tab 切换 ctrl + shift + D 查找所写的函数