Luogu3605 [USACO17JAN]Promotion Counting晋升者计数
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晋升者计数的更多相关文章
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数
P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
- [USACO17JAN]Promotion Counting晋升者计数
题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...
- 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...
- P3605 [USACO17JAN]Promotion Counting晋升者计数
思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...
- BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数
Description The cows have once again tried to form a startup company, failing to remember from past ...
- [USACO17JAN] Promotion Counting晋升者计数 (树状数组+dfs)
题目大意:给你一棵树,求以某节点为根的子树中,权值大于该节点权值的节点数 本题考查dfs的性质 离散+树状数组求逆序对 先离散 我们发现,求逆序对时,某节点的兄弟节点会干扰答案 所以,我们在递推时统计 ...
随机推荐
- Web前端开发必备
前端学习相关书籍 关于书籍 HTML.CSS 类别书籍,都是大同小异,在当当网.卓越网搜索一下很多推荐.如果感觉学的差不多了,可以关注一下<CSS禅意花园>,这个很有影响力. Javasc ...
- HTML5为输入框添加语音输入功能
这里介绍的是大家以后要用到的html强大功能,可直接给输入框增加语音功能,下面我们先来看看实现方法. 大家可以看到在输入框右边的麦克风图标,点击麦克风就能够进行语音识别了. 其实很简单,语音识别是ht ...
- JMeter http(s)请求插件jmeter-plugin-httpBinaryFileUpload.jar
JMeter http(s)请求插件jmeter-plugin-httpBinaryFileUpload.jar by:授客 QQ:1033553122 1. 测试环境 apache-jmeter ...
- "BLAME" is out.
The latest feature animation film "BLAME" is watchable on the Netflix. Rendering was done ...
- fetch数据请求的封装
export default class HttpUtils { static get(url){ return new Promise((resolve,reject)=>{ fetch(ur ...
- win7文件搜索技巧
重要说明 (1)搜索的字符串是大小写不敏感的 (2)字符串带双引号与不带双引号是有区别的 如:hello,搜索包含hello单词开头的文件或目录,名为“aa_HELLOcc...”.“cc-Hel ...
- 银盒子智慧餐厅硬件尺寸规格&推荐机型
- Scala隐式转换
package big.data.analyse.scala import java.io.File import scala.io.Source /** * 隐式转换 * Created by zh ...
- 微信小程序中的循环遍历问题
比如:如果在微信小程序中要遍历输出 0-9 的数,我们会使用for循环 ;i<;i++){ console.log(i); } 确实结果也是这样: 但是,如果我在循环时同时调用wx的api接口1 ...
- C#-简介(一)
1.C#语言简介 C#计算机语言 是一门高级计算机语言 他的开发模式更接近人类和社会的思维模式,有助于提高开发效率 C#历史 1998年COOL这个项目是C#语言的前身,由微软 Anders Hejl ...