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. redo allocation latch redo copy latch

    这两个latch 是干什么的一直有点迷糊,刚才上网查了一下,总结如下: redo allocation latch 在Log Buffer中分配内存空间时需要获取Redo allocation lat ...

  2. Linux模块化机制和module_init

    致谢: 微信公众号:嵌入式企鹅圈 每天都新增爱好者关注,感谢大家的支持和大牛们的建议. 本人将竭力出品很多其它优质的原创文章回馈大家的厚爱. 引子:模块化机制长处 模块化机制(module)是Linu ...

  3. leetcode第一刷_Minimum Window Substring

    好题.字符串.线性时间. 我认为第一次拿到这个题的人应该不会知道该怎么做吧,要么就是我太弱了..先搞清楚这个题要求的是什么.从一个长字符串中找一个字串,这个字串中的字符全然包括了另一个给定目标串中的字 ...

  4. hdu5044(二分)

    题意:一个树上建两个加油站.使得全部点到达其近期加油站的最大距离最小. 解法:二分答案.关键时二分时候,要最合理话布局两个点的位置,做法是处理出来树的直径,然后在直径两端分别向中间移动二分的x步的两个 ...

  5. jeecg-easypoi-2.0.3版本号公布

    EasyPOI是在jeecg的poi模块基础上,继续开发独立出来的,能够说是2.0版本号,EasyPoi封装的目的和jeecg一致,争取让大家write less do more ,在这个思路上eas ...

  6. 转:Java阳历转农历

    package cloud.app.prod.home.utils; import java.text.ParseException; import java.text.SimpleDateForma ...

  7. K度限制MST poj 1639

    /* k度限制MST:有一个点的度<=k的MST poj 1639 要求1号点的度不超过k 求MST 我们先把1号点扔掉 跑MST 假设有sum个连通分支 然后把这sum个分支连到1上 就得到了 ...

  8. 地图使用-----MapKit介绍

    一.MapKit介绍 1.苹果自带地图功能(高德地图),可以提供地图展示,查询,定位,导航等功能.使用MapKit框架实现地图功能,MapKit框架中所有数据类型的前缀都是MK 2.MapKit有一个 ...

  9. codevs1358棋盘游戏(状压dp)

    1358 棋盘游戏  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 大师 Master     题目描述 Description 这个游戏在一个有10*10个格子的棋盘上进行,初 ...

  10. Gym - 101981J The 2018 ICPC Asia Nanjing Regional Contest J.Prime Game 计数

    题面 题意:1e6的数组(1<a[i]<1e6),     mul (l,r) =l × (l+1) ×...× r,  fac(l,r) 代表 mul(l,r) 中不同素因子的个数,求s ...