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. kendo Grid的toolbar自定义

    由于这个toolbar官方进增加了create,save还有一个是_____ 所以想要自定义话就需要使用 下面的代码(这个是MVVM模式) data-toolbar='[{ template: Ken ...

  2. [bzoj1455]罗马游戏_左偏树_并查集

    罗马游戏 bzoj-1455 题目大意:给你n个人,2种操作,m次操作:1.将i号士兵所在的集合的最小值删除 2.合并i和j两个士兵所在的团体 注释:$1\le n\le 10^6$,$1\le m ...

  3. Javascript中JSON的序列化和反序列化(转)

    parse用于从一个字符串中解析出JSON对象,如: var str = '{"name":"easonjim","age":"2 ...

  4. PDF在线预览-pdfjs使用

    请参考我的开源: https://github.com/wuyechun2018/itools/blob/master/src/main/webapp/WEB-INF/views/pdf/index. ...

  5. Android图片异步载入框架Android-Universal-Image-Loader

    Android-Universal-Image-Loader是一个图片异步载入,缓存和显示的框架.这个框架已经被非常多开发人员所使用,是最经常使用的几个Android开源项目之中的一个,主流的应用,随 ...

  6. Zepto Code Rush 2014-A. Feed with Candy(HACK)

    A. Feed with Candy time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. Android push推送消息到达成功率优化

    Android push推送消息到达成功率优化 问题:server向client发送消息.未考虑client是否在线,这种消息到达率是非常低的. 第一次优化:使用server离线缓存数据,推断假设cl ...

  8. 一步步玩pcDuino3--mmc下的bootloader

    pcDuino3下支持mmc启动.官方的Uboot是採用SPL框架实现的,由于内部的SRAM空间达到32K,我们全然能够在这32K空间内编写一个完整可用小巧的bootloader来完毕引导Linux ...

  9. Google android source code build 问题总结【转】

    本文转载自:http://light3moon.com/2015/01/31/Google%20android%20source%20code%20build%20%E9%97%AE%E9%A2%98 ...

  10. ES transport client批量导入

    从bulk.txt文件中按行读取,然后bulk导入.首先通过调用client.prepareBulk()实例化一个BulkRequestBuilder对象,调用BulkRequestBuilder对象 ...