[BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

4756: [Usaco2017 Jan]Promotion Counting

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 305  Solved: 217
[Submit][Status][Discuss]

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

HINT

Source

Platinum鸣谢Acty提供译文

先离散化。

dfs遍历,用树状数组维护,比当前元素小的元素个数,答案显然为子树size-遍历完子树后比当前元素小的元素个数+进入子树时比当前元素小的元素个数。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<algorithm>
 7 using namespace std;
 8 int read() {
 9     int x=0,f=1;char ch=getchar();
10     while(!isdigit(ch)) {ch=getchar();}
11     while(isdigit(ch)) {x=x*10+ch-‘0‘;ch=getchar();}
12     return x;
13 }
14 int n;
15 int p[100001];
16 int a[100002];
17 int sum[100002];
18 int lowbit(int x){return x&(-x);}
19 void update(int x,int val) {for(int i=x;i<=n;i+=lowbit(i)) sum[i]+=val;}
20 int query(int x) {
21     int ans=0;
22     for(int i=x;i>0;i-=lowbit(i)) ans+=sum[i];
23     return ans;
24 }
25 struct data {
26     int to,next;
27 }e[100005];
28 int head[100005],cnt,size[100005],ans[100005];
29 void add(int u,int v) {e[cnt].next=head[u];e[cnt].to=v;head[u]=cnt++;}
30 void dfs(int now) {
31     size[now]=1;
32     int tmp=query(a[now]);
33     update(a[now],1);
34     for(int i=head[now];i>=0;i=e[i].next) {
35         int to=e[i].to;
36         dfs(to);
37         size[now]+=size[to];
38     }
39     tmp=query(a[now])-tmp;
40     ans[now]=size[now]-tmp;
41 }
42 int main() {
43     memset(head,-1,sizeof(head));
44     n=read();
45     for(int i=1;i<=n;i++) p[i]=a[i]=read();
46     sort(p+1,p+n+1);
47     for(int i=1;i<=n;i++) a[i]=lower_bound(p+1,p+n+1,a[i])-p;
48     for(int i=2;i<=n;i++) {
49         int v=i,u=read();
50         add(u,v);
51     }
52     dfs(1);
53     for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
54 }

时间: 2024-08-23 00:08:29

[BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组的相关文章

【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), organi

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

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] 我们考虑每个权值建立一棵线段树,边dfs边将子节点合并为一颗线段树, 那么只要查询当前点的树上后缀和即可. [代码] #include <cstdio> #include <algorithm> #include <cstring> #include <vector&

[USACO17JAN] Promotion Counting晋升者计数 (树状数组+dfs)

题目大意:给你一棵树,求以某节点为根的子树中,权值大于该节点权值的节点数 本题考查dfs的性质 离散+树状数组求逆序对 先离散 我们发现,求逆序对时,某节点的兄弟节点会干扰答案 所以,我们在递推时统计一次答案,递归时再统计一次答案,两者的差值就是最终结果 #include <bits/stdc++.h> #define dd double #define N 100100 using namespace std; int n,cnt,ma,lst; int a[N],head[N],s[N],

P3605 [USACO17JAN]Promotion Counting晋升者计数 线段树合并 or 树状数组

题意:每个点有一个权值    求每个节点的子树中比其权值大的节点数 线段树合并模板题 #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define ll long long #define see(x) (cerr<<(#x)<<'='

13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec  Memory Limit: 128 MB Description In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence.

TOJ 4105 Lines Counting(离线树状数组)

4105.   Lines Counting Time Limit: 2.0 Seconds   Memory Limit: 150000K Total Runs: 152   Accepted Runs: 47 On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you M queries, each query contains two interval

hdu 3887 Counting Offspring dfs序+树状数组

Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description You are given a tree, it’s root is p, and the node is numbered from 1 to n. Now define f(i) as the number of nodes whose numbe

bzoj4397【Usaco2015 Dec】Breed Counting(前缀和、树状数组)

题目描述 Farmer John's N cows, conveniently numbered 1…N, are all standing in a row (they seem to do so often that it now takes very little prompting from Farmer John to line them up). Each cow has a breed ID: 1 for Holsteins, 2 for Guernseys, and 3 for

sdut2610---Boring Counting(离线+树状数组+离散化)

Boring Counting Time Limit: 3000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries, for each query,