https://www.luogu.org/problemnew/show/P2982

这题你写个树剖当然可以做,但是我们还有一种更简单的方法,使用 dfs 序 + 树状数组即可

考虑一只牛到了自己的地方后会对哪些牛产生贡献

当然是它的子树中的牛啊

所以维护一下每个点的 size 和 dfs 序,树状数组维护差分数组,单点查询就转换为前缀查询

#include <bits/stdc++.h>
using namespace std; const int N = 1e5 + 5; struct Edge {
int u, v, next;
}G[N << 1]; int head[N], tops[N], siz[N], f[N];
int n, cnt, tot; inline void addedge(int u, int v) {
G[++tot] = (Edge) {u, v, head[u]}, head[u] = tot;
G[++tot] = (Edge) {v, u, head[v]}, head[v] = tot;
} void dfs(int u, int fa) {
tops[u] = ++cnt, siz[u] = 1;
for(int i = head[u]; i; i = G[i].next) {
int v = G[i].v;
if(v == fa) continue;
dfs(v, u);
siz[u] += siz[v];
}
} int lowbit(int x) {return x & -x;}
void add(int x, int y) {for(int i = x; i <= n; i += lowbit(i)) f[i] += y;}
int query(int x) {int ans = 0; for(int i = x; i; i -= lowbit(i)) ans += f[i]; return ans;} int main() {
cin >> n;
for(int i = 1; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
addedge(a, b);
}
dfs(1, 0);
for(int i = 1; i <= n; i++) {
int a; scanf("%d", &a);
printf("%d\n", query(tops[a]));
add(tops[a], 1);
add(tops[a] + siz[a], -1);
}
return 0;
}

luoguP2982 [USACO10FEB]慢下来Slowing down的更多相关文章

  1. [luoguP2982][USACO10FEB]慢下来Slowing down(dfs序 + 线段树)

    传送门 这个题显然可以用树链剖分做. 然而线段树也能做. 每个点都对它的子树有贡献,所以先求一边 dfs序,然后直接在 dfs序 中搞 线段树 就行. ——代码 #include <cstdio ...

  2. USACO10FEB]慢下来Slowing down dfs序 线段树

    [USACO10FEB]慢下来Slowing down 题面 洛谷P2982 本来想写树剖来着 暴力数据结构直接模拟,每头牛回到自己的农场后,其子树下的所有牛回到农舍时,必定会经过此牛舍,即:每头牛回 ...

  3. 洛谷P2982 [USACO10FEB]慢下来Slowing down [2017年四月计划 树状数组01]

    P2982 [USACO10FEB]慢下来Slowing down 题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) c ...

  4. 洛谷P2982 [USACO10FEB]慢下来Slowing down(线段树 DFS序 区间增减 单点查询)

    To 洛谷.2982 慢下来Slowing down 题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows con ...

  5. [USACO10FEB]慢下来Slowing down

    线段树  树的dfs序 来自   洛谷 P1982   的翻译 by  GeneralLiu 来自 jzyz 的翻译 %mzx 线段树  dfs序 数据结构的应用 “数据结构 是先有需求 再有应用” ...

  6. [luogu2982][USACO10FEB]慢下来Slowing down(树状数组+dfs序)

    题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N mov ...

  7. 洛谷P2982 [USACO10FEB]慢下来Slowing down

    题目 题目大意 :给出一棵树,节点有点权,求每个节点的祖先中点权小于该节点的结点的个数 . 思路如下 : 从根节点开始,对树进行深度优先遍历. 当进行到节点 i 时,有: $\text{i}$ ​的祖 ...

  8. 线段树+Dfs序【p2982】[USACO10FEB]慢下来Slowing down

    Description 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1-N)从粮仓走向他的自己的牧场.牧场构成了一棵树,粮仓在1号牧场.恰好有N-1条道路直 ...

  9. 洛谷 P2982 [USACO10FEB]慢下来Slowing down

    题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N mov ...

随机推荐

  1. WEB扫描器Atscan的安装和使用

    项目地址:https://github.com/AlisamTechnology/ATSCAN root@sch01ar:/sch01ar# git clone https://github.com/ ...

  2. DIKW模型与数据工程

    DIKW 体系 DIKW体系是关于数据.信息.知识及智慧的体系,可以追溯至托马斯·斯特尔那斯·艾略特所写的诗--<岩石>.在首段,他写道:“我们在哪里丢失了知识中的智慧?又在哪里丢失了信息 ...

  3. 后台运行python程序 遇到缓冲区问题

    From: http://www.iteye.com/topic/867446 环境:linux 一段执行时间很长的程序(用python做hive客户端执行mapreduce) 在linux后台执行, ...

  4. Elasticsearch之插件介绍及安装

    ES站点插件(以网页形式展现) 1.BigDesk Plugin (作者 Lukáš Vlček) 简介:监控es状态的插件,推荐![目前不支持2.x] 2.Elasticsearch Head Pl ...

  5. How to fix apt-get GPG error NO_PUBKEY Ubuntu 14

      This morning when I do apt-get update on my new Ubuntu 14.04 server, I got these error messages: R ...

  6. jmeter beanshell

    //获取返回数据 String json = prev.getResponseDataAsString(); ///加入变量vars.put("restr",json); //获取 ...

  7. string基本字符系列容器(二)

    string对象作为vector元素 string对象可以作为vector向量元素,这种用法类似字符串数组. #include<string> #include<vector> ...

  8. 453. Minimum Moves to Equal Array Elements 一次改2个数,变成统一的

    [抄题]: Given a non-empty integer array of size n, find the minimum number of moves required to make a ...

  9. Docker安装和使用Tomcat

    1.搜索Tomcat镜像                          docker search tomcat 2.下载Tomcat官方镜像                   docker p ...

  10. Python 安装urllib3

    一.系统环境 操作系统:Win10 64位 Python版本:Python 3.7.0 二.报错信息 No module named 'urllib3' 三.安装参考 1.参照网上的安装方法通过pip ...