题目描述

The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!

The cows, conveniently numbered $1 \ldots N\left( 1\leq N\leq 100,000\right)$ organize the company as a tree, with cow 1 as the president (the root of the tree). Each cow except the president has a single manager (its "parent" in the tree). Each cow $i$ has a distinct proficiency rating, $p(i)$, which describes how good she is at her job. If cow $i$ is an ancestor (e.g., a manager of a manager of a manager) of cow $j$, then we say $j$ is a subordinate of $i$.

Unfortunately, the cows find that it is often the case that a manager has less proficiency than several of her subordinates, in which case the manager should consider promoting some of her subordinates. Your task is to help the cows figure out when this is happening. For each cow $i$ in the company, please count the number of subordinates $j$ where $p(j)>p(i)$.

奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者!

为了方便,把奶牛从 $1 \ldots N\left( 1\leq N\leq 100,000\right)$ 编号,把公司组织成一棵树,1 号奶牛作为总裁(这棵树的根节点)。除了总裁以外的每头奶牛都有一个单独的上司(它在树上的 “双亲结点”)。所有的第 $i$ 头牛都有一个不同的能力指数 $p(i)$,描述了她对其工作的擅长程度。如果奶牛 $i$ 是奶牛 $j$ 的祖先节点(例如,上司的上司的上司),那么我们我们把奶牛 $j$ 叫做 $i$ 的下属。

不幸地是,奶牛们发现经常发生一个上司比她的一些下属能力低的情况,在这种情况下,上司应当考虑晋升她的一些下属。你的任务是帮助奶牛弄清楚这是什么时候发生的。简而言之,对于公司的中的每一头奶牛 $i$,请计算其下属 $j$ 的数量满足 $p(j)>p(i)$。
输入输出格式
输入格式:

The first line of input contains $N$.

The next NNN lines of input contain the proficiency ratings $p(1) \ldots p(N)$ for the cows. Each is a distinct integer in the range $1 \ldots 1,000,000,000$.

The next $N-1$ lines describe the manager (parent) for cows $2 \ldots N$. Recall that cow 1 has no manager, being the president.

输入的第一行包括一个整数 $N$。

接下来的 $N$ 行包括奶牛们的能力指数 $p(1) \ldots p(N)$. 保证所有数互不相同,在区间 $1 \ldots 10^9$之间。

接下来的 $N−1$ 行描述了奶牛 $2 \ldots N$ 的上司(双亲节点)的编号。再次提醒,1 号奶牛作为总裁,没有上司。

输出格式:

Please print $N$ lines of output. The $i$th line of output should tell the number of subordinates of cow $i$ with higher proficiency than cow $i$.

输出包括 $N$ 行。输出的第 $i$ 行应当给出有多少奶牛 $i$ 的下属比奶牛 $i$ 能力高。

输入输出样例
输入样例#1: 复制

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

输出样例#1: 复制

2
0
1
0
0

说明

感谢@rushcheyo 的翻译

这题有很多做法。但我目前只会一种。慢慢update。

解法一:dfs序+树状数组。

先求出dfs序和每个节点子树中节点个数,然后把所有节点按权值从大到小排。从大到小插入树状数组,插入之前查询一下dfs序区间和就OK了。

#include <bits/stdc++.h>
#define eb emplace_back
using namespace std; inline int read() {
int x = , f = ; char ch = getchar();
while (ch < '' || ch > '') { if (ch == '-') f = -; ch = getchar(); }
while (ch >= '' && ch <= '') { x = x * + ch - ; ch = getchar(); }
return x * f;
} const int N = 1e5 + ;
vector<int> G[N];
struct P {
int u, val;
bool operator < (const P &rhs) const {
return val > rhs.val;
}
} p[N];
int dfn[N], size[N], n, cnt, tree[N]; inline int lowbit(int x) { return x & -x; } void add(int x) {
for (int i = x; i <= n; tree[i]++, i += lowbit(i));
} int query(int x) {
int res = ;
for (int i = x; i; res += tree[i], i -= lowbit(i));
return res;
} void dfs(int u, int fa) {
size[u] = ;
dfn[u] = ++cnt;
for (auto v : G[u]) {
if (v == fa) continue;
dfs(v, u);
size[u] += size[v];
}
} int ans[N]; int main() {
n = read();
for (int i = ; i <= n; i++) {
p[i].val = read();
p[i].u = i;
}
for (int i = ; i <= n; i++) {
int u = read();
G[u].eb(i);
}
dfs(, );
sort(p + , p + n + );
for (int i = ; i <= n; i++) {
int u = p[i].u;
ans[u] = query(dfn[u] + size[u] - ) - query(dfn[u]);
add(dfn[u]);
}
for (int i = ; i <= n; i++) printf("%d\n", ans[i]);
return ;
}

解法二:线段树合并

先离散化,对每个点建一个权值线段树,然后从下往上合并,答案即为当前权值线段树[a[u] + 1, n]的区间和(这玩意好像还挺有趣的?

#include <bits/stdc++.h>
#define eb emplace_back
using namespace std; inline int read() {
int x = , f = ; char ch = getchar();
while (ch < '' || ch > '') { if (ch == '-') f = -; ch = getchar(); }
while (ch >= '' && ch <= '') { x = x * + ch - ; ch = getchar(); }
return x * f;
} const int N = 1e5 + ;
int rt[N], cnt, n, sum[ * N], rs[N * ], ls[N * ], a[N], ans[N];
vector<int> G[N];
struct P {
int v, id;
bool operator < (const P &rhs) const {
return v < rhs.v;
}
} p[N]; inline void pushup(int u) {
sum[u] = sum[ls[u]] + sum[rs[u]];
} void build(int &u, int l, int r, int x) {
if (!u) u = ++cnt;
if (l == r) {
sum[u]++;
return;
}
int mid = l + r >> ;
if (x <= mid) build(ls[u], l, mid, x);
else build(rs[u], mid + , r, x);
pushup(u);
} int merge(int u, int v) {
if (!u || !v) return u + v;
ls[u] = merge(ls[u], ls[v]);
rs[u] = merge(rs[u], rs[v]);
pushup(u);
return u;
} int query(int u, int l, int r, int x, int y) {
if (!u) return ;
if (x <= l && y >= r) return sum[u];
int mid = l + r >> ;
int ans = ;
if (x <= mid) ans += query(ls[u], l, mid, x, y);
if (y > mid) ans += query(rs[u], mid + , r, x, y);
return ans;
} void dfs(int u) {
for (auto v : G[u]) {
dfs(v);
rt[u] = merge(rt[u], rt[v]);
}
ans[u] = query(rt[u], , n, a[u] + , n);
} int main() {
n = read();
for (int i = ; i <= n; i++) {
p[i].v = read();
p[i].id = i;
}
sort(p + , p + n + );
for (int i = ; i <= n; i++) {
a[p[i].id] = i;
build(rt[p[i].id], , n, i);
}
for (int i = ; i <= n; i++) {
int u = read();
G[u].eb(i);
}
dfs();
for (int i = ; i <= n; i++) printf("%d\n", ans[i]);
}

洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数的更多相关文章

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

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

  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. luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...

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

    思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...

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

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

  7. 题解 P3605 【[USACO17JAN]Promotion Counting晋升者计数】

    这道题开10倍左右一直MLE+RE,然后尝试着开了20倍就A了...窒息 对于这道题目,我们考虑使用线段树合并来做. 所谓线段树合并,就是把结构相同的线段树上的节点的信息合在一起,合并的方式比较类似左 ...

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

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

  9. BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数

    Description The cows have once again tried to form a startup company, failing to remember from past ...

随机推荐

  1. logback--日志在windows上可以打印,在linux上不能打印的问题

    解决问题方法: 本人遇到的问题,属于jar包冲突问题.本人加入的jar包如下: slf4j-api-1.7.5.jar slf4j-log4j12-1.7.5.jar logback-core-1.0 ...

  2. JAVA–利用Filter和session防止页面重复提交

    JAVA–利用Filter和session防止页面重复提交解决思路:1 用户访问表单页面,先经过过滤器,过滤器设置一个随机id作为token令牌, 并将该token放入表单隐藏域中.2 表单响应到浏览 ...

  3. 基于layUI调用后台数据实现区域信息级联查询

    基于layUI调用后台数据实现区域信息级联查询 1.基本思路 后台提供根据区域编码查询区域列表公共接口 页面初始化调用后台接口加载所有省份 点击省份将省份区域编码传入后台查询该省份下所有地市信息,以此 ...

  4. DRF框架(三)——media资源路径设置、多表设计复习及补充、序列化组件(ModelSerializer)操作多表(序列化与反序列化)、多表序列化与反序列化整合(重点)

    media资源路径设置  (设置好后把图片放在这个文件夹中,通过链接能访问到图片) 1.先在根目录设置一个media文件夹 2.配置settings.py,加上下面的 MEDIA_URL = '/me ...

  5. 【EBS】取数SQL-平均成本更新的物料事务处理追溯到应付发票

    SELECT hou.name,--组织 aia.invoice_num,--发票编号 msib.segment1,--物料编码 mmt.transaction_id--物料事务处理 FROM mtl ...

  6. Locust性能测试-参数化批量注册

    前言 实现场景:所有并发虚拟用户共享同一份测试数据,并且保证虚拟用户使用的数据不重复. 例如,模拟10用户并发注册账号,总共有100个手机号,要求注册账号不重复,注册完毕后结束测试 准备数据 虚拟用户 ...

  7. C# 运行流程

    转载  https://www.cnblogs.com/qcloud1001/p/9816956.html 从编译原理说起 一句话介绍编译器:编译器是将用某种程式语言写成的源代码(源语言),转换成另一 ...

  8. java之mybatis之使用mybatis实现crud操作

    目录结构: 1.封装 mybatis 的工具类: MybatisUtil.java public class MybatisUtil { private static SqlSessionFactor ...

  9. C#直接调用.mdf文件

    一般情况下,.mdf文件都是作为MSSQL的数据库文件,只有在安装了Microsoft SQL Server才能实现调用. 事实上,除此之外,也可以直接调用.mdf文件,而无需安装Microsoft ...

  10. Charles配置https

    1. 安装Charles ,破解将Charles.jar 覆盖安装路径的\lib 中的文件 2. 查看本机IP 3. 设置手机代理 4. 设置: 5. 手机安装证书 手机浏览器输入网址:chls.pr ...