【树状数组】【Usaco2017 Jan】Promotion Counting

这篇博客介绍了如何利用树状数组解决Usaco2017竞赛中的一道题目——Promotion Counting。题目描述了一个奶牛组成的公司树结构,每个奶牛都有一个能力值,需要找出每个奶牛有多少子节点的能力值比它高。博客内容包括题目背景、输入输出格式、暴力解法和树状数组优化解法的分析,并提供了部分代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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…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号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。

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

分析

首先考虑暴力解法

根据题意,公司是一棵树的形式。

我们遍历每棵子树,对于每个节点,ans[ i ] = 该子树节点数 - 该子树中权值小于当前节点权值的节点数 + 进入子树前符合条件的节点数

考虑使用树状数组维护后两个值

代码

#include<bits/stdc++.h>
using namespace std;
struct edge {
	int v, next;
} e[100010];
int head[100010], cnt;

int n;
int a[100010];
int z[100010];
int tree[100010];
int size[100010];
int ans[100010];

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();}
    return x * f;
}

int ask(int x) {
    int sum = 0;
    for (; x; x -= (x & -x)) sum += tree[x];
    return sum;
}

void add(int x, int y) {
    for (; x <= 100000; x += (x & -x)) tree[x] += y;
}

void insert(int u, int v) {
    e[++cnt].v = v;
    e[cnt].next = head[u];
    head[u] = cnt;
}

void dfs(int u) {
    size[u] = 1;
    int tmp = ask(a[u]);
    add(a[u], 1);
    for (int i = head[u]; i; i = e[i].next) {
	int v = e[i].v;
	dfs(v);
	size[u] += size[v];
    }
    tmp = ask(a[u]) - tmp;
    ans[u] = size[u] - tmp;
}

int main() {
    n = read();
    for (int i = 1; i <= n; i++) a[i] = z[i] = read();
    sort(z + 1, z + n + 1);//离散化STL 
    for (int i = 1; i <= n; i++) a[i] = lower_bound(z + 1, z + n + 1, a[i]) - z;
    for (int i = 1; i < n; i++) {
	int u = read();
	insert(u, i + 1);
    }
    dfs(1);
    for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
}


另外一种方法是线段树合并。。

emmmm这个不会。QAQ。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值