bzoj4756[Usaco2017 Jan]Promotion Counting 离散化+树状数组

Posted GXZlegend

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj4756[Usaco2017 Jan]Promotion Counting 离散化+树状数组相关的知识,希望对你有一定的参考价值。

原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html


题目描述

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…N1…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 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 manager 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 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 ii in the company, please count the number of subordinates jj where p(j)>p(i).
n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。

输入

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号奶牛的经理(树中的父亲)

输出

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大

样例输入

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

样例输出

2
0
1
0
0


题解

离散化+树状数组

先将数据离散化,然后在树上dfs。

搜子树之前求一下比w[x]小的数的个数,搜子树之后再求一下比w[x]小的数的个数,作差即为子树中比w[x]小的数的个数。最后把w[x]加入到树状数组中。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
using namespace std;
struct data
{
	int w , id;
}a[N];
int n , v[N] , pos[N] , f[N] , ans[N] , head[N] , to[N] , next[N] , cnt;
void add(int x , int y)
{
	to[++cnt] = y , next[cnt] = head[x] , head[x] = cnt;
}
bool cmp(data a , data b)
{
	return a.w < b.w;
}
void update(int x , int a)
{
	int i;
	for(i = x ; i <= n ; i += i & -i) f[i] += a;
}
int query(int x)
{
	int i , ans = 0;
	for(i = x ; i ; i -= i & -i) ans += f[i];
	return ans;
}
void dfs(int x)
{
	int i;
	ans[x] -= query(v[x]);
	for(i = head[x] ; i ; i = next[i]) dfs(to[i]);
	ans[x] += query(v[x]);
	update(v[x] , 1); 
}
int main()
{
	int i , x;
	scanf("%d" , &n);
	for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &a[i].w) , a[i].id = i;
	sort(a + 1 , a + n + 1 , cmp);
	for(i = 1 ; i <= n ; i ++ ) v[a[i].id] = n - i + 1;
	for(i = 2 ; i <= n ; i ++ ) scanf("%d" , &x) , add(x , i);
	dfs(1);
	for(i = 1 ; i <= n ; i ++ ) printf("%d\\n" , ans[i]);
	return 0;
}

以上是关于bzoj4756[Usaco2017 Jan]Promotion Counting 离散化+树状数组的主要内容,如果未能解决你的问题,请参考以下文章

bzoj4756[Usaco2017 Jan]Promotion Counting 离散化+树状数组

BZOJ_4756_[Usaco2017 Jan]Promotion Counting_树状数组

BZOJ4756: [Usaco2017 Jan]Promotion Counting

BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)

BZOJ4756: [Usaco2017 Jan]Promotion Counting

bzoj4759 [Usaco2017 Jan]Balanced Photo