题目大意

大小为 \(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的更多相关文章

  1. Luogu3605 [USACO17JAN]Promotion Counting晋升者计数

    Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...

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

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

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

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

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

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

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

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

  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的性质 离散+树状数组求逆序对 先离散 我们发现,求逆序对时,某节点的兄弟节点会干扰答案 所以,我们在递推时统计 ...

  10. [USACO17JAN]Promotion Counting

    线段树合并. 正解好像不是线段树合并,但是出于练手的目的写了线段树合并. 大概就是对于左右子树,如果有一个为空,返回非空的,如果都不为空,就把这两个整合到一起就行了. #include <ios ...

随机推荐

  1. python3中的常见知识点3------reduce()函数

    python3中的常见知识点3--reduce()函数 python3导入reduce()函数 reduce()函数语法 reduce()举例 其他python3常用函数 参考链接 python3中不 ...

  2. Day34:BigDecimal的使用

    BigDecimal 在基本数据类型中对于浮点数的计算时会出现精度丢失的情况,这个时候我们采用BigDecimal类来解决精度丢失的问题. public class Test{ public stat ...

  3. Graph Neural Network——图神经网络

    本文是跟着李沐老师的论文精度系列进行GNN的学习的,详细链接请见:零基础多图详解图神经网络(GNN/GCN)[论文精读] 该论文的标题为<A Gentle Introduction to Gra ...

  4. cs231n__3. LostFunction

    CS231n 3.1 Lost Function 我们上次提到,要如何选择最优的W呢? 这就是要选择几种损失函数了. 我们要找到一种可行的方法来选择最优的W 先看简单的3个样本的例子 正式定义损失函数 ...

  5. 2022NewStarCTF新生赛一些比较有意思的题目wp

    Misc_蚁剑流量分析 Pcap的文件可以直接使用工具 编辑器打开目录,一个一个看,可以找到eval危险函数 看到n3wst4r,直接使用linux正则匹配,找出相关内容 Url解码,了解一下蚁剑流量 ...

  6. 为什么NoSQL数据库这么受欢迎?

    大数据时代,NoSQL数据库是企业构建数据能力的核心工具之一.近期,在2022腾讯全球数字生态大会NoSQL数据库专场上,腾讯云发布了多项NoSQL产品能力升级,并重点讲解了其背后的自研技术要点及实现 ...

  7. K8s 为什么会抛弃 docker

    为什么 K8s 会抛弃 docker 前言 CRI containerd 参考 为什么 K8s 会抛弃 docker 前言 在这之前先来了解下,k8s 是如何和 docker 进行交互的. CRI k ...

  8. angular Ionic CLI项目开始

  9. 结构型模式 - 桥接模式Bridge

    学习而来,代码是自己敲的.也有些自己的理解在里边,有问题希望大家指出. 桥接模式的定义与特点 桥接(Bridge)模式的定义如下:将抽象与实现分离,使它们可以独立变化.它是用组合关系代替继承关系来实现 ...

  10. MyBatis的使用六(解决字段名与成员名不一致)

    本文主要讲述mybatis如何解决mysql的字段名与java实体类的成员变量名称不一致. 一. 介绍实体类和数据表 1. 实体类Employee public class Employee { pr ...