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. PHP文件操作(二)-文件的读取

    1.fread()    //读取打开的文件 fread(file,length) file:必选项,规定要读取的打开的文件 length:必选项,规定要读取的最大字节数. <?php $fil ...

  2. Class.forName和ClassLoader.loadClass区别(转)

    Java中class是如何加载到JVM中的:1.class加载到JVM中有三个步骤    装载:(loading)找到class对应的字节码文件.    连接:(linking)将对应的字节码文件读入 ...

  3. 21-从零玩转JavaWeb-多态详解

    配套视频详解 多态思想 eclipse快捷键设置 多态的好处 多态方法调用 instanceof关键字 多态中字段注意点 一.什么是多态   既然子类是一种特殊的父类   那么我们可不可以认为   狗 ...

  4. LINQ GroupBy 查询数据赋给select

    roles.GroupBy(a => new { a.SubjectID,a.SubjectName}).Select(p => new SelectListItem() { Value ...

  5. layer使用总结一配置

    导入layer.js文件即可,必须先导入jquery.js文件,因为layer是基于jquery 版本匹配,在此记录一下,layer使用1.8下载时是2.3的版本,对应的jquery使用1.8.3版本 ...

  6. Spring 实例化bean的三种方式

    第一种方法:直接配置Bean <bena id="所需要实例化的一个实例名称" class="包名.类名"/> 例如: 配置文件中的bean.XML ...

  7. ubunt 14.04 Could not find CMAKE_ROOT !!! CMake has most likely not been installed correctly. Modul

    CMake Error: Could not find CMAKE_ROOT !!! CMake has most likely not been installed correctly. Modul ...

  8. ROS naviagtion analysis: costmap_2d--Costmap2D

    博客转载自:https://blog.csdn.net/u013158492/article/details/50492506 Costmap2D是存储地图数据的父类.真正的地图数据就存储在数据成员u ...

  9. Java方法学习疑问

    此方法不理解 finalize() 方法 Java允许定义这样的方法,它在对象被垃圾收集器析构(回收)之前调用,这个方法叫做finalize( ),它用来清除回收对象. 例如,你可以使用finaliz ...

  10. 932F Escape Through Leaf

    传送门 题目大意 https://www.luogu.org/problemnew/show/CF932F 分析 我们可以从叶子向根每次插入b和ans 所以我们不难发现就是相当于插入线段 于是李超树+ ...