NC24727 [USACO 2010 Feb G]Slowing down
题目
题目描述
Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N move from the barn to her private pasture. The pastures are organized as a tree, with the barn being on pasture 1. Exactly N-1 cow unidirectional paths connect the pastures; directly connected pastures have exactly one path. Path i connects pastures AiA_iAi and BiB_iBi (1 <= <= N; 1 <= <= N).
Cow i has a private pasture PiP_iPi (1 <= <= N). The barn's small door lets only one cow exit at a time; and the patient cows wait until their predecessor arrives at her private pasture. First cow 1 exits and moves to pasture . Then cow 2 exits and goes to pasture , and so on.
While cow i walks to she might or might not pass through a pasture that already contains an eating cow. When a cow is present in a pasture, cow i walks slower than usual to prevent annoying her friend.
Consider the following pasture network, where the number between parentheses indicates the pastures' owner. 1 (3) / \ (1) 4 3 (5) / \ (2) 2 5 (4) First, cow 1 walks to her pasture: 1 (3) / \ [1] 4* 3 (5) / \ (2) 2 5 (4) When cow 2 moves to her pasture, she first passes into the barn's pasture, pasture 1. Then she sneaks around cow 1 in pasture 4 before arriving at her own pasture. 1 (3) / \ [1] 4* 3 (5) / \ [2] 2* 5 (4) Cow 3 doesn't get far at all -- she lounges in the barn's pasture, #1. 1* [3] / \ [1] 4* 3 (5) / \ [2] 2* 5 (4) Cow 4 must slow for pasture 1 and 4 on her way to pasture 5: 1* [3] / \ [1] 4* 3 (5) / \ [2] 2* 5* [4] Cow 5 slows for cow 3 in pasture 1 and then enters her own private pasture: 1* [3] / \ [1] 4* 3*[5] / \ [2] 2* 5* [4]
FJ would like to know how many times each cow has to slow down.
输入描述
- Line 1: Line 1 contains a single integer: N
- Lines 2..N: Line i+1 contains two space-separated integers: and
- Lines N+1..N+N: line N+i contains a single integer:
输出描述
- Lines 1..N: Line i contains the number of times cow i has to slow down.
示例1
输入
5 1 4 5 4 1 3 2 4 4 2 1 5 3
输出
0 1 0 2 1
题解
知识点:DFS序,树状数组。
我们需要求,一个牛到他去的地方的路径上经过了几个已经有牛的地方。用树剖很容易解决路径查询、单点修改问题。但是这里用dfs序一样可以维护,只是我们需要将单点修改转换为对其子树答案的修改,即一个牛到达之后其子树的答案都会加 ,查询就会变成单点查询。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> using namespace std; using ll = long long; template<class T> class Fenwick { int n; vector<T> node; public: Fenwick(int _n = 0) { init(_n); } void init(int _n) { n = _n; node.assign(n + 1, T()); } void update(int x, T val) { for (int i = x;i <= n;i += i & -i) node[i] += val; } T query(int x) { T ans = T(); for (int i = x;i >= 1;i -= i & -i) ans += node[i]; return ans; } }; struct T { int sum; T &operator+=(const T &x) { return sum += x.sum, *this; } }; const int N = 1e5 + 7; vector<int> g[N]; int p[N]; int dfncnt; int L[N], R[N]; void dfs(int u, int fa) { L[u] = ++dfncnt; for (auto v : g[u]) { if (v == fa) continue; dfs(v, u); } R[u] = dfncnt; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 1;i <= n - 1;i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1;i <= n;i++) cin >> p[i]; dfs(1, 0); Fenwick<T> fw(n); for (int i = 1;i <= n;i++) { cout << fw.query(L[p[i]]).sum << '\n'; fw.update(L[p[i]], { 1 }); fw.update(R[p[i]] + 1, { -1 }); } return 0; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/17499433.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
2022-06-23 NC50038 kotori和糖果
2022-06-23 Contest
2022-06-23 NC19115 选择颜色
2022-06-23 NC23046 华华教月月做数学
2022-06-23 NC14731 逆序对
2022-06-23 NC15052 求最值
2022-06-23 NC50999 表达式计算4