首页 > 其他分享 >NC24727 [USACO 2010 Feb G]Slowing down

NC24727 [USACO 2010 Feb G]Slowing down

时间:2023-06-23 17:44:15浏览次数:57  
标签:Feb her cow int contains Slowing down she pasture

题目链接

题目

题目描述

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;
}

标签:Feb,her,cow,int,contains,Slowing,down,she,pasture
From: https://www.cnblogs.com/BlankYang/p/17499433.html

相关文章

  • Markdown学习
    Markdown学习标题三级标题四级标题 字体Hello,World!Hello,World!Hello,World!Hello,World! 引用选择狂神说Java,走向人生巅峰 分割线三个-,或者三个* 图片 超链接点击跳转到犬汰twitter  列表ABCABC 表格 名字......
  • R数据分析:解决科研中的“可重复危机”,理解Rmarkdown
    不知道刚接触科研的大伙儿有没有这么一个感觉,别人的研究很大可能你重复不出来,尤其是社科实证研究,到现在我都还觉得所谓的实证是个很玄乎的东西;如果是刚开始做数据分析,很多时候你会发现自己的分析结果过几天自己都重复不出来。反正我自己是有这样的经历的。有可能是某一步操作忘......
  • 代码项目快速生成markdown文件
    code2md/run_img2markdown.command#!/bin/bashsource/Users/song/Code/script_python/code2md/venv/bin/activate#echo-n'请任意拖入文件夹中的一个文件:'#readfile_pathpython3/Users/song/Code/script_python/code2md/main_img2markdown.pycode2md/main......
  • Markdown使用
    一般这样来写大标题一般这样来写一个小标题这样来写粗体字这样写斜体字加粗斜体字删除线最重要的:代码块,有时候也可以用来表示颜色取分(粉色的)大的代码块markdown的参考手册无法对它加粗暂时\(\color{blue}{蓝色的}\)1.这是有序列表2.这是有序列表3.这是有序列......
  • keycloak~CountDownLatch在keycloak中的使用
    概念在Java中,CountDownLatch是一个线程同步的辅助类,用于等待其他线程完成操作。如果CountDownLatch实例被丢失或无法访问,可能会导致无法正常使用该对象。这可能会导致等待线程永远处于等待状态,无法继续执行。如果意外丢失了CountDownLatch对象,你可以尝试以下方法进行恢复或处理:......
  • 【快应用】nativeAd.onStatusChanged和nativeAd.onDownloadProgress接口正确监听广告
    【关键词】原生广告、下载监听、状态返回【问题背景】快应用接入原生广告后,通过nativeAd.onStatusChanged和nativeAd.onDownloadProgress接口来监听广告下载状态和进度,但是在广告触发下载后,没有回调返回。该如何解决?代码:showNativeAd(){nativeAd=ad.createNativeAd({a......
  • Turndown 源码分析:五、节点相关`root-node.js`和`node.js`
    importcollapseWhitespacefrom'./collapse-whitespace'importHTMLParserfrom'./html-parser'import{isBlock,isVoid}from'./utilities'//单独构造的根节点,防止输入字符串含有多个根元素exportdefaultfunctionRootNode(input,options){var......
  • MONGODB 复制集 DOWN DOWN 机了, 5种情况与系统恢复
    最近TEAM里面的每个DB都在做高可用失效后的应急方案和处理的文档,要写这个东西我和MONGODB的DBA主要要做的有以下内容1 环境的准备三台MOGNODB4.2 社区版本2  安装成为复制集3  制定测试计划     测试计划主要从以下几个方面考虑   1  从库DOWN机,对应......
  • Turndown 源码分析:二、规则`commonmark-ruiles.js` REV1
    import{repeat}from'./utilities'varrules={}//段落rules.paragraph={filter:'p',replacement:function(content){//前后加两个换行return'\n\n'+content+'\n\n'}}//换行rules.lineBrea......
  • Turndown 源码分析:四、`turndown.js`
    importCOMMONMARK_RULESfrom'./commonmark-rules'importRulesfrom'./rules'import{extend,trimLeadingNewlines,trimTrailingNewlines}from'./utilities'importRootNodefrom'./root-node'importNodefrom'......