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

一道水题,本来可以用dfs+树状数组两次查询的方式水过的,这里为了练习线段树合并又自己yy了一个,结果还是不错的

因为RE了一回,本地又卡系统栈特意又去查了下空间复杂度,原来每次合并最多会新开logN个节点,所以就开到NlogN就好了

不过要是实在蒙圈的话我认为开到内存上限也不失为良策,只不过要千万小心的计算内存啊。。。

 #include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
int n,m,p[N],t[N],ans[N];
int h[N],to[N],nxt[N],etop;
void add(int u,int v){to[++etop]=v;nxt[etop]=h[u];h[u]=etop;}
int data[N*],ls[N*],rs[N*],tot;
int newtree(int l,int r,int x){
data[++tot]=;
if(l==r)return tot;
int node=tot,mid=(l+r)>>;
if(x<=mid)ls[node]=newtree(l,mid,x);
else rs[node]=newtree(mid+,r,x);
return node;
}
int merge(int l,int r,int u,int v){
if(!u||!v)return u+v;
if(l==r){
data[++tot]=data[u]+data[v];
return tot;
}
int mid=(l+r)>>,node=++tot;
ls[node]=merge(l,mid,ls[u],ls[v]);
rs[node]=merge(mid+,r,rs[u],rs[v]);
data[node]=data[ls[node]]+data[rs[node]];
return node;
}
int query(int l,int r,int L,int R,int u){
if(!u)return ;
if(L>R)return ;
if(l==L&&r==R)return data[u];
int mid=(l+r)>>;
if(R<=mid)return query(l,mid,L,R,ls[u]);
else if(L>mid)return query(mid+,r,L,R,rs[u]);
else return query(l,mid,L,mid,ls[u])+query(mid+,r,mid+,R,rs[u]);
}
int dfs(int u){
int node=newtree(,n,p[u]),v;
for(int k=h[u];k;k=nxt[k]){
v=dfs(to[k]);
node=merge(,n,node,v);
}
ans[u]=query(,n,p[u]+,n,node);
return node;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&p[i]),t[i]=p[i];
sort(t+,t++n);
m=unique(t+,t++n)-t-;
for(int i=;i<=n;i++)p[i]=lower_bound(t+,t++m,p[i])-t;
for(int i=,fff;i<=n;i++){
scanf("%d",&fff);
add(fff,i);
}
dfs();
for(int i=;i<=n;i++)
printf("%d\n",ans[i]);
}

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

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

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

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

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

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

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

  9. [USACO17JAN] Promotion Counting晋升者计数 (树状数组+dfs)

    题目大意:给你一棵树,求以某节点为根的子树中,权值大于该节点权值的节点数 本题考查dfs的性质 离散+树状数组求逆序对 先离散 我们发现,求逆序对时,某节点的兄弟节点会干扰答案 所以,我们在递推时统计 ...

随机推荐

  1. [poj1363]Rails_模拟_栈

    Rails poj-1363 题目大意:判断一个序列是否是1~n的合法出栈序列. 注释:$1\le n\le 10^4$. 想法:开始想到一种想法. 对于一段序列来讲,显然从首元素开始的连续小于尾元素 ...

  2. assign retain 和copy的区别

    assign 对基础数据类型 (NSInteger,CGFloat)和C数据类型(int, float, double, char)等 等. 此标记说明设置器直接进⾏行赋值,这也是默认值.在使⽤用垃圾 ...

  3. Openfire:解决乱码问题

    当部署openfire后,创建用户和发送离线消息时会出现中文字符乱码的问题.要解决这个问题需要同时配置openfire和mysql两端. 首先openfire端,在安装页面中指定odbc连接串中需要带 ...

  4. ioctl在socket中的一些用法及示例

    原文: http://blog.chinaunix.net/uid-20692625-id-3172833.html ----------------------------------------- ...

  5. 一张图告诉你Git的所有命令

  6. NOI 2009A 诗人小G

    NOI 2009A 诗人小G 诗人小G [问题描述] 小G是一个出色的诗人,经常作诗自娱自乐.但是,他一直被一件事情所困扰,那就是诗的排版问题. 一首诗包含了若干个句子,对于一些连续的短句,可以将它们 ...

  7. python nltk 入门demo

    sudo pip install -U pyyaml nltk import nltk nltk.download() 搞不定,必须代理: Installing via a proxy web ser ...

  8. 91. ExtJS获取父子、兄弟容器元素方法

    转自:https://blog.csdn.net/u014745818/article/details/44957341 1 1.当前对象的父对象(上级对象) this.ownerCt: 2.当前对象 ...

  9. Prime Path(bfs)

    http://poj.org/problem?id=3126 题意:给两个四位数n,m,将n变成m需要多少步,要求每次只能改变n的某一位数,即改变后的数与改变前的数只有一位不同,且每次改变后的数都是素 ...

  10. Spark2.0.2+Zeppelin0.6.2 环境搭建 初探

    0.抱怨与其他(此部分与标题没有太多联系): 首先一点想说的是版本问题,为什么标题我会写清楚版本号呢!原因就是版本不对真的很会坑人. 就在写这篇博客的同一天,我还写了另一篇,是 Hadoop2.7.3 ...