[USACO17JAN]Promotion Counting P
题目大意
大小为 \(n\) 以 \(1\) 为根的树,点带权,求每个子树内大于本点的点的数量
\(1 \le n \le 10^5,1 \le p_i \le 10^9\)
题解
一眼静态链分治,然后统计?
离散化后树状数组维护即可
时间复杂度 \(n \log ^2 (n)\)
\(Code\)
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 1e5 + 5;
int n, h[N], tot;
struct edge{int to, nxt;}e[N];
inline void add(int x, int y){e[++tot] = edge{y, h[x]}, h[x] = tot;}
struct node{int p, id;}a[N];
inline bool cmp1(node x, node y){return x.p < y.p;}
inline bool cmp2(node x, node y){return x.id < y.id;}
int dfc, fa[N], siz[N], dfn[N], rev[N], son[N], ans[N];
void dfs1(int x)
{
siz[x] = 1, dfn[x] = ++dfc, rev[dfc] = x;
for(register int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
dfs1(v), siz[x] += siz[v];
if (siz[v] > siz[son[x]]) son[x] = v;
}
}
int c[N];
inline int lowbit(int x){return x & (-x);}
inline void update(int x, int v){for(; x <= n; x += lowbit(x)) c[x] += v;}
inline int query(int x)
{
int res = 0;
for(; x; x -= lowbit(x)) res += c[x];
return res;
}
void dfs2(int x, int kp)
{
for(register int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == son[x]) continue;
dfs2(v, 0);
}
if (son[x]) dfs2(son[x], 1);
for(register int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == son[x]) {update(a[son[x]].p, 1); continue;}
for(register int j = dfn[v]; j <= dfn[v] + siz[v] - 1; j++) update(a[rev[j]].p, 1);
}
ans[x] = siz[x] - 1 - query(a[x].p);
if (!kp) for(register int i = dfn[x] + 1; i <= dfn[x] + siz[x] - 1; i++) update(a[rev[i]].p, -1);
}
int main()
{
scanf("%d", &n);
for(register int i = 1; i <= n; i++) scanf("%d", &a[i].p), a[i].id = i;
sort(a + 1, a + n + 1, cmp1);
for(register int i = 1; i <= n; i++) a[i].p = i;
sort(a + 1, a + n + 1, cmp2);
for(register int i = 2; i <= n; i++) scanf("%d", &fa[i]), add(fa[i], i);
dfs1(1), dfs2(1, 1);
for(register int i = 1; i <= n; i++) printf("%d\n", ans[i]);
}
[USACO17JAN]Promotion Counting P的更多相关文章
- Luogu3605 [USACO17JAN]Promotion Counting晋升者计数
Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...
- 线段树合并 || 树状数组 || 离散化 || 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 ...
- 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的性质 离散+树状数组求逆序对 先离散 我们发现,求逆序对时,某节点的兄弟节点会干扰答案 所以,我们在递推时统计 ...
- [USACO17JAN]Promotion Counting
线段树合并. 正解好像不是线段树合并,但是出于练手的目的写了线段树合并. 大概就是对于左右子树,如果有一个为空,返回非空的,如果都不为空,就把这两个整合到一起就行了. #include <ios ...
随机推荐
- SSH(三)创建包和第一个页面
在ssh web项目src下一次创建 com.ssh/ .action .dao .entity .service 四个包,example: 在entity包下创建class,name: produc ...
- jquery操作class
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- K8s安装乐维5.0应用部署文档
乐维产品包具体打包为4个镜像包,分别为:mysql5.7.36.tar.zabbix_server.tar.itops_v1_4_x86_64.tar.bpm0.1.tar,对应的配置文件分别为:da ...
- 批量将多个相同Excel表格内容合并到一个Excel表格的sheet工作簿当中。
Sub Books2Sheets()Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) Dim ...
- JavaScript中的防抖与节流-图文版
01.防抖还是节流 防抖 与 节流 目的都是避免一定时间内,大量重复的操作造成的性能损耗.因此原理也类似,都是阻止过多的事件执行,只保留一部分来执行.适用场景略有不同,也有交叉,动手练习一遍就懂了. ...
- 【机器学习】李宏毅——Unsupervised Learning
读这篇文章之间欢迎各位先阅读我之前写过的线性降维的文章.这篇文章应该也是属于Unsupervised Learning的内容的. Neighbor Embedding Manifold Learnin ...
- JavaScript:严格模式"use strict"
因为历史遗留问题,JS其实存在很多feature,以及兼容性问题: 所以JS在ES5之后,新增了一个严格模式,以区别于普通模式,用来激活新的特性,使得某些代码的执行准确无误: 如何开启严格模式? 在J ...
- Web应用怎样获取Access Token?
1.在联盟创建服务器应用 参考文档:开发准备 2.获取用户级Access Token 2.1 获取code 参考文档:接入华为帐号获取凭证 2.1.1 先按照跳转链接进行配置url https://o ...
- java RSA加密
参考了下面这个博主的文章,很有收获,简单处理后记录一下 RSA加密.解密.签名.验签的原理及方法 - PC君 - 博客园 工具类自带生成秘钥的方法,也可以用第三方工具生成秘钥 package com. ...
- Redis 数据结构-简单动态字符串
Redis 数据结构-简单动态字符串 无边落木萧萧下,不尽长江滚滚来. 1.简介 Redis 之所以快主要得益于它的数据结构.操作内存数据库.单线程和多路 I/O 复用模型,进一步窥探下它常见的五种基 ...