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. window.parent与window.opener、window.showModalDialog的区别 opener和showModalDialog刷新父页面的方法

    项目中使用案例: 父窗体 <s:form namespace="/forexagent" id="listSearchForm" name="t ...

  2. [转]十五天精通WCF——第七天 Close和Abort到底该怎么用才对得起观众

    一:文起缘由 写这一篇的目的源自于最近看同事在写wcf的时候,用特别感觉繁琐而且云里雾里的嵌套try catch来防止client抛出异常,特别感觉奇怪,就比如下面的代码. public void S ...

  3. [jQuery]ajax请求导致浏览器崩溃

    $("#xxx").val() not $("#xxx") 如果忘记加上.val()会导致chrome崩溃

  4. ASP.NET MVC 提供与訪问 Web Api

    ASP.NET MVC 提供与訪问 Web Api 一.提供一个 Web Api 新建一个项目.类型就选 "Web Api". 我用的是MVC5,结果生成的项目一大堆东西.还编译只 ...

  5. 【转】C语言条件编译及编译预处理阶段

    原文: http://www.cnblogs.com/rusty/archive/2011/03/27/1996806.html 1. 宏定义(宏代换,宏替换,宏: 宏定义是C语言提供的3中预处理功能 ...

  6. AVPlayer的使用,带缓冲

    #import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...

  7. [Angulalr] Speed Up Reducer Development Using Ngrx Schematics

    When we use NGRX, we need to create some bolipates. Now with Angulalr6, we can use CLI to generate t ...

  8. http自己定义超时检測方法、主动抛出异常

    上次为了解决无网路由器超时的问题,将问题重心转移到了HttpClient. HttpUrLConnection上面,什么各种设置ReadTimeout. connectionTimeOut,还有所谓的 ...

  9. ListView点击或选中item改变背景

    点击或选中ListView中的一项后.使item背景改变,失去焦点相同显示选中的背景.又一次选中另外一项才刷新: 在Adapter中配置: public class MyAdapter extends ...

  10. IOS6.0自带下拉刷新控件UIRefreshControl

    1.UIRefreshControl必需要在IOS6.0以后才干使用,同一时候他仅仅能在UITableViewController类中才干够使用 2.使用比較简单 self.refreshContro ...