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 <= \(A_i\) <= N; 1 <= \(B_i\) <= N).
Cow i has a private pasture PiP_iPi (1 <= \(P_i\) <= 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 \(P_1\). Then cow 2 exits and goes to pasture \(P_2\) , and so on.
While cow i walks to \(P_i\) 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: \(A_i\) and \(B_i\)
- Lines N+1..N+N: line N+i contains a single integer: \(P_i\)
输出描述
- 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序一样可以维护,只是我们需要将单点修改转换为对其子树答案的修改,即一个牛到达之后其子树的答案都会加 \(1\) ,查询就会变成单点查询。
时间复杂度 \(O(n \log n)\)
空间复杂度 \(O(n)\)
代码
#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;
}
NC24727 [USACO 2010 Feb G]Slowing down的更多相关文章
- NC24724 [USACO 2010 Feb S]Chocolate Eating
NC24724 [USACO 2010 Feb S]Chocolate Eating 题目 题目描述 Bessie has received \(N (1 <= N <= 50,000)\ ...
- BZOJ1782[USACO 2010 Feb Gold 3.Slowing down]——dfs+treap
题目描述 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1…N)从粮仓走向他的自己的牧场.牧场构成了一棵树,粮仓在1号牧场.恰好有N-1条道路直接连接着牧场, ...
- [ USACO 2010 FEB ] Slowing Down
\(\\\) \(Description\) 给出一棵 \(N\) 个点的树和 \(N\) 头牛,每头牛都要去往一个节点,且每头牛去往的点一定互不相同. 现在按顺序让每一头牛去往自己要去的节点,定义一 ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- USACO翻译:USACO 2014 FEB SILVER 三题
USACO 2014 FEB SILVER 一.题目概览 中文题目名称 自动打字 路障 神秘代码 英文题目名称 auto rblock scode 可执行文件名 auto rblock scode 输 ...
- [USACO 2018 Feb Gold] Tutorial
Link: USACO 2018 Feb Gold 传送门 A: $dp[i][j][k]$表示前$i$个中有$j$个0且末位为$k$的最优解 状态数$O(n^3)$ #include <bit ...
- USACO 2009 Feb 股票市场 Stock Market
USACO 2009 Feb 股票市场 Stock Market Description 尽管奶牛们天生谨慎,她们仍然在住房抵押信贷市场中大受打击,现在她们准备在股市 上碰碰运气.贝西开挂了,她知道S ...
- NC25025 [USACO 2007 Nov G]Sunscreen
NC25025 [USACO 2007 Nov G]Sunscreen 题目 题目描述 To avoid unsightly burns while tanning, each of the \(C\ ...
- USACO 2012 Feb Cow Coupons
2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MB Submit: 349 Solved: 181 [Su ...
- usaco 2010年3月银组题解
usaco银组解题报告 一.石子游戏如果把‘O’当作0,‘X’当做1,则N个洞的每一种状态都可以看做是一个N位二进制数.于是,这个问题就变成了求环绕的N位格雷码.幸运的是,这个结构很容易就能够用一个简 ...
随机推荐
- 《DREEAM Guiding Attention with Evidence for Improving Document-Level Relation Extraction》阅读笔记
代码 原文地址 预备知识: 1.什么是K-L散度(Kullback-Leibler Divergence)? K-L散度,是一种量化两种概率分布P和Q之间差异的方式,又叫相对熵.在概率学和统计 ...
- spring启动流程 (1) 流程概览
本文将通过阅读AnnotationConfigApplicationContext源码,分析Spring启动流程. 创建AnnotationConfigApplicationContext Annot ...
- docker容器中执行GPU环境中的tensorflow和pytorch任务
1. 背景 (1) 业务方提供了一台有GPU的服务器,且已经安装了显卡等组件,cuda版本10.2,具体信息如下 (2) 在裸机上部署anaconda.pytorch.tensorflow较为麻烦,因 ...
- 【深入解读Redis系列】(五)Redis中String的认知误区,详解String数据类型
有时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,请认准https://blog.zysicyj.top 首发博客地址 系列文章地址 需求描述 现在假设有这样一个需求,我们要开发一个 ...
- [转帖]为非root用户添加NOPASSWD权限
https://www.jianshu.com/p/d1e71bda4b34 查看树莓派默认是怎么为pi用户免去密码 所有配置文件都在 /etc 目录下,免去密码配置文件也不例外.在/etc/sudo ...
- [转帖]Codis作者黄东旭:细说分布式Redis架构设计和那些踩过的坑
https://dbaplus.cn/news-141-270-1.html Codis是一个分布式Redis解决方案,与官方的纯P2P模式不同,Codis采用的是Proxy-based的方案.今天我 ...
- [转帖]CentOS7安装笔记:minio分布式集群搭建
文章目录 准备机器 部署(所有机器均执行) 创建挂载磁盘路径 挂载磁盘路径到文件系统 创建minio目录 下载minio安装包 创建启动脚本 创建启动服务 启动测试(所有机器执行) 重新加载服务的配置 ...
- 【转帖】3.JVM内存结构概述
目录 1.JVM内存结构 1.JVM内存结构 在JVM系列的第一篇文章中已经给出了JVM内存结构的简图,下面是JVM内存结构更加详细的图. 同样,JVM的内存结构可以分为上中下3层. 上层主要是类加载 ...
- [转帖]Linux小知识:sudo su和su的区别
https://www.cnblogs.com/jiading/p/11717388.html su是申请切换root用户,需要申请root用户密码.有些Linux发行版,例如ubuntu,默认没有设 ...
- date的命令使用.
date命令的使用 1.直接用date命令显示日期时间 在命令行中输入date然后回车,显示结果"Wed Aug 7 08:58:07 CST 2019".这是系统根据设定的时区显 ...