Description

The cows have once again tried to form a startup company, failing to remember from past experience t
hat cows make terrible managers!The cows, conveniently numbered 1…N1…N (1≤N≤100,000), organize t
he company as a tree, with cow 1 as the president (the root of the tree). Each cow except the presid
ent has a single manager (its "parent" in the tree). Each cow ii has a distinct proficiency rating, 
p(i), which describes how good she is at her job. If cow ii is an ancestor (e.g., a manager of a man
ager 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 seve
ral of her subordinates, in which case the manager should consider promoting some of her subordinate
s. 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).
n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。

Input

The first line of input contains N
The next N lines of input contain the proficiency ratings p(1)…p(N) 
for the cows. Each is a distinct integer in the range 1…1,000,000,000
The next N-1 lines describe the manager (parent) for cows 2…N 
Recall that cow 1 has no manager, being the president.
n,表示有几只奶牛 n<=100000
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)

Output

Please print N lines of output. The ith line of output should tell the number of 
subordinates of cow ii with higher proficiency than cow i.
共n行,每行输出奶牛i的下属中有几个能力值比i大

Sample Input

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

Sample Output

2
0
1
0
0
 
线段树合并
代码:
 #include<iostream>
#include<cstdio>
#include<algorithm>
#define M 100010
#define ls ch[node][0]
#define rs ch[node][1]
using namespace std; int n,m,num,cnt;
int head[M],a[M],b[M],ans[M],rt[M];
int v[M<<],ch[M<<][];
struct point{int to,nxt;}e[M<<]; void add(int from,int to)
{
e[++num].nxt=head[from];
e[num].to=to;
head[from]=num;
} void insert(int &node,int l,int r,int x)
{
if(!node) node=++cnt;v[node]++;
if(l==r) return;
int mid=(l+r)/;
if(x<=mid) insert(ls,l,mid,x);
else insert(rs,mid+,r,x);
} int query(int node,int l,int r,int l1,int r1)
{
if(!node) return ;
if(l1<=l&&r1>=r) return v[node];
if(l1>r||r1<l) return ;
int mid=(l+r)/;
return query(ls,l,mid,l1,r1)+query(rs,mid+,r,l1,r1);
} int merge(int x,int y)
{
if(!x||!y) return x+y;
int node=++cnt;
v[node]=v[x]+v[y];
ls=merge(ch[x][],ch[y][]);
rs=merge(ch[x][],ch[y][]);
return node;
} void dfs(int x)
{
for(int i=head[x];i;i=e[i].nxt)
{
int to=e[i].to;
dfs(to);
rt[x]=merge(rt[x],rt[to]);
}
ans[x]=query(rt[x],,m,a[x]+,m);
insert(rt[x],,m,a[x]);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]),b[++m]=a[i];
sort(b+,b++m); m=unique(b+,b++m)-b-;
for(int i=;i<=n;i++) a[i]=lower_bound(b+,b++m,a[i])-b;
for(int i=;i<=n;i++)
{
int x;scanf("%d",&x);
add(x,i);
}
dfs();
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
return ;
}

[BZOJ4756]Promotion Counting的更多相关文章

  1. [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

    4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: ...

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

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

  3. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu 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. 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数

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

  6. 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

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

  7. BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)

    题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...

  8. BZOJ4756:[USACO]Promotion Counting(线段树合并)

    Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...

  9. bzoj4756 [Usaco2017 Jan]Promotion Counting

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...

随机推荐

  1. Netty 应用程序的一个一般准则:尽可能的重用 EventLoop,以减少线程创建所带来的开销。

    Netty 系列一(核心组件和实例). - JMCui - 博客园 https://www.cnblogs.com/jmcui/p/9154842.html 阅读目录 一.概念 二.核心组件 三.实例 ...

  2. super()函数的用法

    http://www.runoob.com/python/python-func-super.html class FooParent(object): def __init__(self): sel ...

  3. 前端开发 - HTML

    1.index2.head标签相关内容3.常用标签一4.常用标签二 table5.常用标签二 form6.标签分类 1.index <!--声明文档的类型 标记该文档为HTML5的文件--> ...

  4. list文档

    文档 class list(object): """ list() -> new empty list list(iterable) -> new list ...

  5. python 面向对象(进阶篇)转载武沛齐

    上一篇<Python 面向对象(初级篇)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使 ...

  6. spring 拾遗

    1.@PostConstruct VS  init-method 1.1 both BeanPostProcessor 1.2 @PostConstruct is a JSR-250 annotati ...

  7. JavaWeb-HttpServletResponse对象一

    web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象,和代表响应的response对象.resquest和response对象既然代表请求和响应,那么要 ...

  8. weblogic中eclipse远程调试

    1. weblogic 配置文件修改 修改文件: weblogic/weblogic103/user_projects/domains/xxxx/bin/setDomainEnv.sh(windows ...

  9. spring MVC中的异常统一处理

    1.spring MVC中定义了一个标准的异常处理类SimpleMappingExceptionResolver 该类实现了接口HandlerExceptionResolver 2.看下SimpleM ...

  10. C语言可以分配的最大内存

    前言 最近用C刷PAT算法题目, 发现C语言有太多需要关注大小范围的东西必须 知道, 虽说挺麻烦, 但也挺有意思. int最大值是多少 首先就是int类型的取值范围, 这个太常用. C语言标准规定最低 ...