洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
Promotion Counting
题目描述
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 N1…N (1 \leq N \leq 100,0001≤N≤100,000), 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 ii has a distinct proficiency rating, p(i)p(i), which describes how good she is at her job. If cow ii is an ancestor (e.g., a manager of a manager of a manager) of cow jj, then we say jj is a subordinate of ii.
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 ii in the company, please count the number of subordinates jj where p(j) > p(i)p(j)>p(i).
奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者!
为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N≤100,000) 编号,把公司组织成一棵树,1 号奶牛作为总裁(这棵树的根节点)。除了总裁以外的每头奶牛都有一个单独的上司(它在树上的 “双亲结点”)。所有的第 ii 头牛都有一个不同的能力指数 p(i)p(i),描述了她对其工作的擅长程度。如果奶牛 ii 是奶牛 jj 的祖先节点(例如,上司的上司的上司),那么我们我们把奶牛 jj 叫做 ii 的下属。
不幸地是,奶牛们发现经常发生一个上司比她的一些下属能力低的情况,在这种情况下,上司应当考虑晋升她的一些下属。你的任务是帮助奶牛弄清楚这是什么时候发生的。简而言之,对于公司的中的每一头奶牛 ii,请计算其下属 jj 的数量满足 p(j) > p(i)p(j)>p(i)。
输入输出格式
输入格式:
The first line of input contains NN.
The next NN lines of input contain the proficiency ratings p(1) \ldots p(N)p(1)…p(N) for the cows. Each is a distinct integer in the range 1 \ldots 1,000,000,0001…1,000,000,000.
The next N-1N−1 lines describe the manager (parent) for cows 2 \ldots N2…N. Recall that cow 1 has no manager, being the president.
输入的第一行包括一个整数 NN。
接下来的 NN 行包括奶牛们的能力指数 p(1) \cdots p(N)p(1)⋯p(N). 保证所有数互不相同,在区间 1 \cdots 10^91⋯109 之间。
接下来的 N-1N−1 行描述了奶牛 2 \cdots N2⋯N 的上司(双亲节点)的编号。再次提醒,1 号奶牛作为总裁,没有上司。
输出格式:
Please print NN lines of output. The iith line of output should tell the number of subordinates of cow ii with higher proficiency than cow ii.
输出包括 NN 行。输出的第 ii 行应当给出有多少奶牛 ii 的下属比奶牛 ii 能力高。
输入输出样例
5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
2
0
1
0
0
说明
感谢@rushcheyo 的翻译
分析:
线段树合并。
先构建出树形结构,然后对每一个节点建立一个权值线段树,在$dfs$过程中,对于每一个子节点把它的每一个子节点的线段树与它的线段树合并。查询答案就在当前线段树中查询比该节点的权值$val[x]$更大的值有多少个即可。
Code:
//It is made by HolseLee on 15th Oct 2018
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int N=1e5+;
int n,head[N],cnte,val[N],root[N],tot,cnt,seg[N*],ls[N*],rs[N*],ans[N];
struct Node {
int id,v;
inline bool operator < (const Node x) const {
return v<x.v;
}
}b[N];
struct Edge {
int to,nxt;
}e[N]; inline int read()
{
char ch=getchar(); int num=; bool flag=false;
while( ch<'' || ch>'' ) {
if( ch=='-' ) flag=true; ch=getchar();
}
while( ch>='' && ch<='' ) {
num=num*+ch-''; ch=getchar();
}
return flag ? -num : num;
} inline void add(int x,int y)
{
e[++cnte].to=y;
e[cnte].nxt=head[x];
head[x]=cnte;
} void build(int &rt,int l,int r,int x)
{
if( !rt ) rt=++tot;
seg[rt]++;
if( l==r ) return;
int mid=(l+r)>>;
if( x<=mid ) build(ls[rt],l,mid,x);
else build(rs[rt],mid+,r,x);
} int query(int rt,int l,int r,int x)
{
if( !rt ) return ;
if( x<=l ) return seg[rt];
int mid=(l+r)>>;
if( x<=mid ) return query(ls[rt],l,mid,x)+query(rs[rt],mid+,r,x);
else return query(rs[rt],mid+,r,x);
} int merge(int x,int y)
{
if( !x || !y ) return x+y;
int rt=++tot;
seg[rt]=seg[x]+seg[y];
ls[rt]=merge(ls[x],ls[y]);
rs[rt]=merge(rs[x],rs[y]);
return rt;
} void dfs(int x)
{
for(int i=head[x],y; i; i=e[i].nxt) {
y=e[i].to; dfs(y);
root[x]=merge(root[x],root[y]);
}
ans[x]=query(root[x],,cnt,val[x]+);
build(root[x],,cnt,val[x]);
} int main()
{
n=read();
int x,y;
for(int i=; i<=n; ++i) b[i].id=i, b[i].v=read();
sort(b+,b+n+);
for(int i=; i<=n; ++i)
if( b[i].v>b[i-].v ) val[b[i].id]=++cnt;
else val[b[i].id]=cnt;
for(int i=; i<=n; ++i) {
x=read(); add(x,i);
}
dfs();
for(int i=; i<=n; ++i) printf("%d\n",ans[i]);
return ;
}
洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]的更多相关文章
- 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数
P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...
- luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...
- P3605 [USACO17JAN]Promotion Counting晋升者计数
思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...
- 【USACO17JAN】Promotion Counting晋升者计数 线段树+离散化
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- Luogu3605 [USACO17JAN]Promotion Counting晋升者计数
Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...
- [USACO17JAN]Promotion Counting晋升者计数
题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...
- 「洛谷4197」「BZOJ3545」peak【线段树合并】
题目链接 [洛谷] [BZOJ]没有权限号嘤嘤嘤.题号:3545 题解 窝不会克鲁斯卡尔重构树怎么办??? 可以离线乱搞. 我们将所有的操作全都存下来. 为了解决小于等于\(x\)的操作,那么我们按照 ...
随机推荐
- Java并发编程原理与实战一:聊聊并发
一.大纲 •你真的了解并发吗 •多线程和并发 •多线程和多进程 •线程一定快吗 •学习并发的四个阶段 •学习目标 •适合人群 •荐书 二.学习并发的四个阶段 •熟练掌握API,能够完成并发编程 • ...
- scrapy爬取天气数据
看了scrapy,打算构建自己的天气数据,目标源:就是你了,中国天气网! 仔细点两下这个网站,发现可以由各个省.直辖市到省市所属的地级市,再到各县,页面在这: 点开就可以看到中国所有的省.直辖市,但港 ...
- phpunit安装出错的原因及解决办法
官方指引 很遗憾, phpunit还没有在ArchLinux的仓库里. 所以使用下载安装的方式.按照官方的指引: wget https://phar.phpunit.de/phpunit.phar c ...
- bzoj 2726 [SDOI2012]任务安排(斜率DP+CDQ分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2726 [题意] 将n个任务划分成若干个块,每一组Mi任务花费代价(T+sigma{ t ...
- java学习第02天(语言基础组成:关键字、标识符、注释、常量和变量)
Java语言基础组成 1. 关键字 就是指的一些单词,这些单词被赋予了特殊的java含义,就不再叫单词了. 例如: class Demo{ public static void main(String ...
- [洛谷P1228]地毯填补问题 题解(分治)
Description 相传在一个古老的阿拉伯国家里,有一座宫殿.宫殿里有个四四方方的格子迷宫,国王选择驸马的方法非常特殊,也非常简单:公主就站在其中一个方格子上,只要谁能用地毯将除公主站立的地方外的 ...
- 关于UDP数据报引发“异步错误”的疑问
在UNP卷一第三版的第8章8.9小节中说到:如果udp服务器没有启动,udp客户端在使用sendto发送一行文本后,将会等待一个永远也不会出现的应答从而阻塞在recvfrom调用上. 由于服务器段不存 ...
- 移动开发关于APN的知识整理
APN(Access Point Name),即"接入点名称",用来标识GPRS的业务种类,是通过手机上网时必须配置的一个参数,其决定了手机通过哪种接入方式来访问网络. 一.类别 ...
- 编写高效的JavaScript程序
作者: Addy Osmani 来源: CSDN 发布时间: 2013-01-10 14:15 阅读: 7952 次 推荐: 15 原文链接 [收藏] 英文原文:Writing Fas ...
- XSS注入常用语句
<script>alert('hello,gaga!');</script> //经典语句,哈哈! >"'><img src="javas ...