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. Socket编程(网络编程)

    网络通信的第一要素:IP地址 通过IP地址唯一的定位到互联网的主机 通过 IP+port(端口号) 来确定互联网的主机上的某个软件 InetAddress:位于java.net包下 getHostNa ...

  2. for、for / in循环

    1.for循环 循环代码块一定的次数 <!DOCTYPE html> <html lang="en" dir="ltr"> <he ...

  3. 洛谷P1516 青蛙的约会

    题目描述 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清 ...

  4. 一个简单的scrollTop动画的方法

    var autoScrollTop = function (param) { var delay = param.scrollDom.height() * 20; param.dom.animate( ...

  5. ASP.NET MVC Json的序列化和反序列化

    1.利用js进行序列化成字符串和反序列化 var personObj = {name:"Tom",age:16}; // 利用JS序列化成字符串 var personStr = J ...

  6. python itchat 爬取微信好友信息

    原文链接:https://mp.weixin.qq.com/s/4EXgR4GkriTnAzVxluJxmg 「itchat」一个开源的微信个人接口,今天我们就用itchat爬取微信好友信息,无图言虚 ...

  7. 转载:量化投资中常用python代码分析(一)

    pandas的IO 量化投资逃不过数据处理,数据处理逃不过数据的读取和存储.一般,最常用的交易数据存储格式是csv,但是csv有一个很大的缺点,就是无论如何,存储起来都是一个文本的格式,例如日期‘20 ...

  8. (办公)重新选择一个开发工具Eclipse

    文章Eclipse内容摘抄自w3cschool的eclipse,原文地址:https://www.w3cschool.cn/eclipse/eclipse-run-configuration.html ...

  9. Linux Xshell连接Linux服务器时报错Socket error Event: 32 Error: 10053

    问题描述 在用Xshell连接Linux服务器时,出现错误提示"Socket error Event: 32 Error: 10053. Connection closing...Socke ...

  10. ajax请求经典格式

    $.ajax({ url: url, type: "POST", dataType: "json", data: data, success: funtion1 ...