【USACO 2017 Jan】Promotion Counting

【题目】

Description

The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!The cows, conveniently numbered 1…N (1≤N≤100,000), organize the company as a tree, with cow 1 as the president (the root of the tree). Each cow except the president has a single manager (its “parent” in the tree). Each cow i has a distinct proficiency rating,p(i), which describes how good she is at her job. If cow i is an ancestor (e.g., a manager of a manager of a manager) of cow j, then we say j is a subordinate of i.

Unfortunately, the cows find that it is often the case that a manager has less proficiency than several of her subordinates, in which case the manager should consider promoting some of her subordinates. Your task is to help the cows figure out when this is happening. For each cow i in the company, please count the number of subordinates j where p(j)>p(i).

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.

Output

Please print N lines of output. The ith line of output should tell the number of subordinates of cow i with higher proficiency than cow i.

Sample Input

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

Sample Output

2
0
1
0
0


【分析】

题目大意:给出一棵树(根为 1 1 ),每个点有点权,对于每个点,询问它子树中点权比它大的点的个数

其实可以把这道题当成线段树合并入门题来做

首先把点权离散化,把所有的点都先单独建一颗权值线段树

然后从根开始 d f s dfs ,一边 d f s dfs 一边合并线段树,不断合并上去,最后求答案就行了


【代码】

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
using namespace std;
int n,m,t,tot;
int first[N],v[N],nxt[N];
int a[N],ans[N],val[N],root[N],size[N*40],lc[N*40],rc[N*40];
void add(int x,int y)
{
	t++;
	nxt[t]=first[x];
	first[x]=t;
	v[t]=y;
}
void build(int &root,int l,int r,int val)
{
	root=++tot;
	size[root]=1;
	if(l==r)  return;
	int mid=(l+r)>>1;
	if(val<=mid)  build(lc[root],l,mid,val);
	else  build(rc[root],mid+1,r,val);
}
int Merge(int x,int y)
{
	if(!x)  return y;
	if(!y)  return x;
	size[x]+=size[y];
	lc[x]=Merge(lc[x],lc[y]);
	rc[x]=Merge(rc[x],rc[y]);
	return x;
}
int calc(int root,int l,int r,int x,int y)
{
	if(l>=x&&r<=y)
	  return size[root];
	int ans=0,mid=(l+r)>>1;
	if(x<=mid)  ans+=calc(lc[root],l,mid,x,y);
	if(y>mid)  ans+=calc(rc[root],mid+1,r,x,y);
	return ans;
}
void dfs(int x)
{
	int i,j;
	for(i=first[x];i;i=nxt[i])
	{
		j=v[i],dfs(j);
		root[x]=Merge(root[x],root[j]);
	}
	ans[x]=calc(root[x],1,m,val[x]+1,m);
}
int main()
{
	int x,i;
	scanf("%d",&n);
	for(i=1;i<=n;++i)  scanf("%d",&a[i]),val[i]=a[i];
	for(i=2;i<=n;++i)  scanf("%d",&x),add(x,i);
	sort(a+1,a+n+1),m=unique(a+1,a+n+1)-(a+1);
	for(i=1;i<=n;++i)
	{
		val[i]=lower_bound(a+1,a+m+1,val[i])-a;
		build(root[i],1,m,val[i]);
	}
	dfs(1);
	for(i=1;i<=n;++i)
	  printf("%d\n",ans[i]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/forever_dreams/article/details/83474346
今日推荐