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. springboot成神之——spring jdbc的使用

    本文介绍spring jdbc的使用 目录结构 pom配置 properties配置 model层User类 Dao层QueryForListDao config层AppConfiguration 程 ...

  2. 8.solr学习速成之FacetPivot

    什么是Facet.pivot  Facet.pivot就是按照多个维度进行分组查询,是Facet的加强,在实际运用中经常用到,一个典型的例子就是商品目录树 NamedList解释: NamedList ...

  3. 15 更多jQuery知识

    jqueryUI 官网: https://jqueryui.com/ jqueryUI 中文网: http://www.jqueryui.org.cn/ jquery插件内容包含 官网demo: ht ...

  4. IDE 文件查找快捷键被占

    快捷键 Ctrl+Shift+F 被搜狗输入法抢占了. 打开搜狗输入法里的快捷键设置,去掉,my ide is ok!

  5. winform 如何正确的获取窗体的标题栏高度

    最近我需要知道鼠标在一个控件里的相对位置,鼠标相对于屏幕的位置我是可以知道的,所以只要得到控件相对于屏幕的位置,就可以算出鼠标相对于控件的位置了 但是发现有误差 后来经过测试是由于窗体的标题栏高度导致 ...

  6. ParksLink修改密码

    设置环境变量: ?set classpath=D:\ptc\PartsLink\srclib\jmxcore\WtLogR.jar;D:\ptc\PartsLink\srclib\log4j.jar; ...

  7. 开坑数位dp

    [背景] 在10月3日的dp专练中,压轴的第6题是一道数位dp,于是各种懵逼. 为了填上这个留存已久的坑,蒟蒻chty只能开坑数位dp了. [例题一][HDU2089]不要62 题目大意:给你一个区间 ...

  8. XHProf安装使用笔记

    编译安装 获取源代码包root@sourcjoy>wget http://pecl.php.net/get/xhprof-0.9.2.tgz解压root@sourcjoy>tar zxf ...

  9. 367. Valid Perfect Square判断是不是完全平方数

    [抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  10. ROS naviagtion analysis: costmap_2d--ObstacleLayer

    博客转载自:https://blog.csdn.net/u013158492/article/details/50493676 构造函数 ObstacleLayer() { costmap_ = NU ...