Luogu3605 [USACO17JAN]Promotion Counting晋升者计数

给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) 。对于每个 \(i\) ,求 \(\displaystyle\sum_{j\in subtree(i)}{[a_i<a_j]}\)

\(n\leq10^5,\ fa_i<i\)

树状数组


树上逆序对?一眼线段树合并、、、空间没毛病、、

回想求序列逆序对的过程,发现在树上做时只需减去树状数组中以往的贡献,于是便可以愉快地树状数组搞了

时间复杂度 \(O(n\log n)\) ,空间复杂度 \(O(n)\)

#include <bits/stdc++.h>
using namespace std; #define nc getchar()
const int maxn = 1e5 + 10;
int n, a[maxn], dat[maxn], c[maxn], ans[maxn]; vector <int> e[maxn]; inline int read() {
int x = 0; char c = nc;
while (c < 48) c = nc;
while (c > 47) x = x * 10 + c - 48, c = nc;
return x;
} void upd(int pos) {
for (; pos <= n; pos += pos & -pos) {
c[pos]++;
}
} int query(int pos) {
int res = 0;
for (; pos; pos &= pos - 1) {
res += c[pos];
}
return res;
} void dfs(int u) {
int lst = query(n) - query(a[u]); upd(a[u]);
for (int v : e[u]) dfs(v);
ans[u] = query(n) - query(a[u]) - lst;
} int main() {
n = read();
for (int i = 1; i <= n; i++) {
dat[i] = a[i] = read();
}
for (int i = 2; i <= n; i++) {
e[read()].push_back(i);
}
sort(dat + 1, dat + n + 1);
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(dat + 1, dat + n + 1, a[i]) - dat;
}
dfs(1);
for (int i = 1; i <= n; i++) {
printf("%d\n", ans[i]);
}
return 0;
}

Luogu3605 [USACO17JAN]Promotion Counting晋升者计数的更多相关文章

  1. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  2. 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数

    P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...

  3. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  4. [USACO17JAN]Promotion Counting晋升者计数

    题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...

  5. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  6. luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...

  7. P3605 [USACO17JAN]Promotion Counting晋升者计数

    思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...

  8. BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数

    Description The cows have once again tried to form a startup company, failing to remember from past ...

  9. [USACO17JAN] Promotion Counting晋升者计数 (树状数组+dfs)

    题目大意:给你一棵树,求以某节点为根的子树中,权值大于该节点权值的节点数 本题考查dfs的性质 离散+树状数组求逆序对 先离散 我们发现,求逆序对时,某节点的兄弟节点会干扰答案 所以,我们在递推时统计 ...

随机推荐

  1. jQuery效果之封装一个文章图片弹出放大效果

    首先先搭写一个基本的格式: $.fn.popImg = function() { //your code goes here } 然后用自调用匿名函数包裹你的代码,将系统变量以变量形式传递到插件内部, ...

  2. BZOJ2434: [Noi2011]阿狸的打字机(AC自动机 树状数组)

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4140  Solved: 2276[Submit][Status][Discuss] Descript ...

  3. Java虚拟机(五)Java的四种引用级别

    1.前言 HotSpot采取了可达性分析算法用来判断对象是否被能被GC,无论是引用计算法还是可达性分析算法都是判断对象是否存在引用来判断对象是否存活.如果reference类型的数据中存储的数值代表的 ...

  4. Docker & pure-ftpd 快速加建 FTP 服务器

    项目需要进行升级服务,现在需要基于centos 7使用docker来快速打架一个FTP环境来方便本地文件上传. 本次使用的是 pure-ftpd docker镜像,有关镜像使用的详细信息,本人是从 h ...

  5. NumPy的使用(一)

    # -*- coding: utf8 -*- from numpy import* a=arange(15).reshape(3,5) print a print a.shape print a.nd ...

  6. spring4笔记----PropertyOverrideConfigureer 重写占位符配置器(图)

  7. mssql instead of 触发器应用一-创建只读视图(view)的方法

    转自: http://www.maomao365.com/?p=4906 <span style="color:white;background-color:blue;font-wei ...

  8. 【audio】耳机插拔 线控按键识别流程【转】

    耳机插拔/线控按键识别流程 耳机插拔/线控按键识别流程 1.文档概述 本文以msm8909平台,android N为例,介绍了通用情况下,耳机插拔的流程步骤,以及对耳机类型的识别逻辑.以方便在项目工作 ...

  9. git-------基础知识(本地推送项目版本---github上)

    创建Git仓库 一:初始化版本库:-git init 二:添加文件到缓存区:-git add  --添加所有文件 是:加个点-列:git add . 三:查看仓库状态:-git status 四:添加 ...

  10. jvm结构

    JVM的基本结构及其各部分详解(一)  https://www.cnblogs.com/zwbg/p/6194470.html JVM的基本结构及其各部分详解(二) https://www.cnblo ...