博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组
阅读量:4473 次
发布时间:2019-06-08

本文共 2781 字,大约阅读时间需要 9 分钟。

原文地址: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 
#include
#include
#define N 100010using 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;}

转载于:https://www.cnblogs.com/GXZlegend/p/6832263.html

你可能感兴趣的文章
IP通信作业1 抓包协议分析
查看>>
android 4.2 root
查看>>
shiro整合SpringMVC
查看>>
连接SQL Server数据库语法
查看>>
python yield
查看>>
软件工程——第四次作业(4)
查看>>
.NET平台下WEB应用程序的部署(安装数据库和自动配置)
查看>>
c++ 符号执行顺序小例子
查看>>
共同父域下的单点登录
查看>>
在Linux(Luna)下向Launch启动器中添加图标
查看>>
【LeetCode】7、Reverse Integer(整数反转)
查看>>
插入排序
查看>>
winsock2之最简单的win socket编程
查看>>
vissim 加载 arcgis shape 文件 坐标误差
查看>>
POJ 1577 Falling Leaves(二叉搜索树)
查看>>
前端cookie
查看>>
简单推导
查看>>
Python之匿名函数,循环
查看>>
网站从 IIS6 迁移至 IIS7 后的通配符脚本映射权限配置不当可能导致403错误
查看>>
DB2如何将数据库表解锁
查看>>