题目大意

大小为 \(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. php+apache环境搭建

    [先安装apache] apache快速安装:https://www.cnblogs.com/brad93/p/16718104.html PHP安装教程参考:https://www.cnblogs. ...

  2. 3.4:使用Weka实现KNN分类的算法示例

    〇.概述 1.使用Weka平台,并在该平台使用数据导入.可视化等基本操作: 2.对KNN算法的不同k值进行比较,对比结果得出结论. 一.打开Weka3.8并导入数据 二.导入数据 三.KNN算法分类操 ...

  3. 【Shell脚本案例】案例1:服务器系统配置初始化

    〇.目录 一.背景 新购买10台服务器,并安装Linux系统 目的:对操作系统进行配置的初始化 二.需求 1.设置时区并同步时间 2.禁用selinux安全机制 3.关闭防火墙(清空防火墙的默认策略, ...

  4. 重学c#系列—— 反射深入一点点[三十三]

    前言 在上一章中介绍了什么是反射: https://www.cnblogs.com/aoximin/p/16440966.html 正文 上一节讲述反射的基本原理和为什么要用反射,还用反射的优缺点这些 ...

  5. 2、Java封装、继承与多态

    /** * 类.对象.面向过程.面向对象的理解: * 1.类:类是封装对象的属性和方法的载体 * * 2.对象:对象是类抽象出来的一个实例 * * 3.面向过程:面向过程注重的是具体的实现过程,因果关 ...

  6. 实践GoF的23种设计模式:命令模式

    摘要:命令模式可将请求转换为一个包含与请求相关的所有信息的对象, 它能将请求参数化.延迟执行.实现 Undo / Redo 操作等. 本文分享自华为云社区<[Go实现]实践GoF的23种设计模式 ...

  7. HBase详解(04) - HBase Java API使用

    HBase详解(04) - HBase Java API使用 环境准备 新建Maven项目,在pom.xml中添加依赖 <dependency> <groupId>org.ap ...

  8. 如何在Github上创建一个新仓库

    Hi,欢迎大家在有空的时候做客[江涛学编程],这里是2023年的第6篇原创文章,新年新气象,在这里我祝读者朋友们都好好的, 老规矩,拍拍手,上菜. 今天没有啥东西要跟家人们分享,就两个字,看图!!! ...

  9. python之路48 django 视图层、模板层

    视图层之必会三板斧 用来处理请求的视图函数都必须返回HttpResponse对象 完全正确 class HttpResponse: pass return HttpResponse() def ren ...

  10. [C++]const_cast,dynamic_cast,reinterpret_cast,static_cast转型

    C++四种新式转型: const_cast(expression) dynamic_cast(expression) reinterpret_cast(expression) static_cast( ...