题目链接

题目

题目描述

The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!

The cows, conveniently numbered 1…N (\(1\leq N\leq 100,000\)), organize the company as a tree, with cow 1 as the president (the root of the tree). Each cow except the president has a single manager (its "parent" in the tree). Each cow i has a distinct proficiency rating, p(i), which describes how good she is at her job. If cow i is an ancestor (e.g., a manager of a manager of a manager) of cow j, then we say j is a subordinate of i.

Unfortunately, the cows find that it is often the case that a manager has less proficiency than several of her subordinates, in which case the manager should consider promoting some of her subordinates. Your task is to help the cows figure out when this is happening. For each cow i in the company, please count the number of subordinates j where p(j)>p(i).

输入描述

The first line of input contains N.

The next N lines of input contain the proficiency ratings p(1)…p(N) for the cows. Each is a distinct integer in the range 1…1,000,000,000.

The next N−1 lines describe the manager (parent) for cows 2…N. Recall that cow 1 has no manager, being the president.

输出描述

Please print N lines of output. The ith line of output should tell the number of subordinates of cow i with higher proficiency than cow i.

示例1

输入

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

输出

2
0
1
0
0

题解

知识点:DFS序,树状数组。

普通逆序对的公式是:

\[\sum_{i=1}^n \sum_{j<i} [a_j > a_i]
\]

即从左到右枚举用时间轴维护先后关系,用树状数组维护数字大小关系。

类似地,我们求出树的dfn序,得到以 \(u\) 为根节点的子树的开始和结束序号 \(L_u,R_u\) ,则树上逆序对公式是:

\[\sum_{i=1}^n \sum_{L_i\leq j \leq R_i} [a_j > a_i]
\]

其中 \(L_i \leq j \leq R_i\) 需要时间轴的版本,单纯枚举是处理不了的。我们可以考虑用时间轴维护大小关系,先将 \(a\) 从大到小排序,那么公式转换为:

\[\sum_{i = 1}^n \sum_{a_j > a_i} [L_i \leq j \leq R_i]
\]

其中 \(\displaystyle \sum_{a_j > a_i} [L_i \leq j \leq R_i]\) ,在时间轴维护大小关系下,用树状数组即可求出区间出现过的数字个数。

时间复杂度 \(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;
}
T query(int l, int r) {
T ans = T();
ans += query(r);
ans -= query(l - 1);
return ans;
}
}; struct T {
int sum;
T &operator+=(const T &x) { return sum += x.sum, *this; }
T &operator-=(const T &x) { return sum -= x.sum, *this; }
}; const int N = 100007;
pair<int, int> a[N];
vector<int> g[N]; int dfncnt;
int L[N], R[N];
void dfs(int u) {
L[u] = ++dfncnt;
for (auto v : g[u]) dfs(v);
R[u] = dfncnt;
} int ans[N]; int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i].first, a[i].second = i;
sort(a + 1, a + n + 1, greater<pair<int, int>>());
for (int i = 2;i <= n;i++) {
int u;
cin >> u;
g[u].push_back(i);
}
dfs(1); Fenwick<T> fw(n);
for (int i = 1;i <= n;i++) {
int u = a[i].second;
ans[u] = fw.query(L[u], R[u]).sum;
fw.update(L[u], { 1 });
} for (int i = 1;i <= n;i++) cout << ans[i] << '\n';
return 0;
}

NC24048 [USACO 2017 Jan P]Promotion Counting的更多相关文章

  1. [USACO 2017 Jan Gold] Tutorial

    Link: 传送门 A: 按值大小插入后用树状数组统计两边个数 #include <bits/stdc++.h> using namespace std; #define X first ...

  2. 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

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

  3. [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

    4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: ...

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

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

  5. USACO翻译:USACO 2012 JAN三题(2)

    USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...

  6. USACO翻译:USACO 2012 JAN三题(1)

    USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...

  7. USACO翻译:USACO 2013 JAN三题(1)

    USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...

  8. USACO翻译:USACO 2014 JAN三题(2)

    USACO 2014 JAN 一.题目概览 中文题目名称 队伍平衡 滑雪录像 滑雪场建设 英文题目名称 bteams recording skicourse 可执行文件名 bteams recordi ...

  9. USACO翻译:USACO 2014 JAN三题(1)

    USACO 2014 JAN 一.题目概览 中文题目名称 滑雪场设计 滑雪降速 滑雪场评级 英文题目名称 skidesign slowdown skilevel 可执行文件名 skidesign sl ...

  10. USACO 2017 January Platinum

    因为之前忘做了,赶紧补上. T1.Promotion Counting 题目大意:给定一个以1为根的N个节点的树(N<=100,000),每个节点有一个权值,对于每个节点求出权值比它大的子孙的个 ...

随机推荐

  1. 小白学标准库之 http

    1. 前言 标准库是工具,是手段,是拿来用的.一味的学标准库就忽视了语言的内核,关键.语言层面的特性,内存管理,垃圾回收.数据结构,设计模式.这些是程序的内核,要熟练,乃至精通它们,而不是精通标准库. ...

  2. idea新建spring boot 项目右键无package及java类的选项

    新创建的spring boot项目,只有一个默认的资源目录及启动配置. 在 group 的目录下右键新建包路径时 ,发现没有package选项,也没有Java Class的选项: 解决办法: File ...

  3. 基于python+django的酒店预定网站-酒店管理系统

    该系统是基于python+django开发的酒店预定管理系统.适用场景:大学生.课程作业.毕业设计.学习过程中,如遇问题可在github给作者留言. 演示地址 前台地址: http://hotel.g ...

  4. 【Altium Designer】五颜六色标识的PCB布板(增强PCB可视化特性)

    出现上图中五颜六色的网络标识,对比各个网络会更加清晰,实现步骤如下 打开或关闭  View--->Net Color Override Active   快捷键     F5 设置 displa ...

  5. [转帖]Windows设置WiFi走外网,有线网卡走内网。

    https://www.itblogcn.com/article/1844.html   文章目录 设置笔记本 WIFI 走外网,网线走内网: 1.查看路由表: 2.删除默认路由: 3.添加 WiFi ...

  6. [转帖]Linux 页表、大页与透明大页

    一. 内存映射与页表 1. 内存映射 我们通常所说的内存容量,指的是物理内存,只有内核才可以直接访问物理内存,进程并不可以. Linux 内核给每个进程都提供了一个独立的虚拟地址空间,并且这个地址空间 ...

  7. [转帖] Jmeter学习笔记(七)——监听器元件之察看结果树

    https://www.cnblogs.com/pachongshangdexuebi/p/11507289.html 在jmeter中,如果我们需要查看请求结果就需要添加查看结果树,这个监听器元件有 ...

  8. [转帖]JMeter压测Redis

    https://www.cnblogs.com/yjlch1016/p/14052402.html 一.Redis Data Set插件: https://jmeter-plugins.org/wik ...

  9. [转帖]PostgreSQL(三) 内存参数优化和原理(work_mem)内存表 pgfincore插件使用方法

    1.常用内存参数 1.1 shared_buffers shared_buffers是PostgreSQL用于共享缓冲区的内存,是由8kb大小的块所形成的数组.PostgreSQL在进行更新.查询等操 ...

  10. [转帖] Linux文本命令技巧(下)

    https://www.cnblogs.com/codelogs/p/16060108.html 简介# 前一篇介绍了Linux中一些基本的文本命令与使用技巧,但是结合场景过少,本篇结合工作中一些常见 ...