Luo Valley P3605 [USACO17JAN] Promotion Counting promoted by counting


题目描述

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 \ldots N\left( 1\leq N\leq 100,000\right)$ 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) $.

the cows once again trying to create a start-up company, still did not learn from past experiences lessons - cattle are terrible managers!

For convenience, the cows from $ 1 \ ldots N \ left ( 1 \ leq N \ leq 100,000 \ right) $ number, the company organized into a tree, cow No. 1 as president (the tree root). In addition to other than the CEO each cow has a single boss (it tree "parent node"). All of the $ i $ cows have a different capability index $ p (i) $, she describes the extent of their good work. If the cow is a cow $ i $ $ j $ ancestor node (for example, your boss's boss's boss), then we put our cows $ j $ is called $ i $ subordinates.

Unfortunately, the cows find a boss often lower than some of her subordinates capacity situation, in this case, the boss should be considered for promotion to some of her subordinates. Your task is to help the cow to figure out what time it happened. Briefly, for each company in the cow $ I $, Calculate the number of subordinates which satisfies $ J $ $ p (j)> p ( i) $.
Input and output format
input format:

Of The First Line of INPUT the contains $ N $.

Of The Next NNN Lines of INPUT Contain The Proficiency ratings $ P (. 1) \ ldots P (N) $ for The Cows. Each IS A DISTINCT Integer in The Range $. 1 \ ldots 1,000,000,000 $.

the next $ N-1 $ lines describe the manager (parent) for cows $ 2 \ ldots N $. Recall that cow 1 has no manager, being the president.

the first input line comprises an integer $ N $.

The next line contains $ N $ cows capability index $ p (1) \ ldots p (N) $. Ensure that all numbers different from each other, between 1 \ ldots 10 ^ 9 $ $ in the interval.

The next $ N-1 $ line describes cows $ 2 \ ldots N $ boss (parent node) number. Again, cow No. 1 as president, no boss.

Output format:

. Please Lines of Print $ N $ $ I $ TH of The Output of Output Line Number of Should Tell The subordinates of Cow with IN AREAS OF COMMUNICAITIONS $ $ I $ I $ Proficiency Within last Cow.

Output line comprises $ N $. $ I $ the first line of the output should be given the number of cows $ i $ subordinates than cows $ i $ capabilities.

Sample Input Output
Input Sample # 1: Copy

5
804 289 384
846 930 887
681 692 778
714 636 916
957 747 794
1
1
2
3

Output Sample # 1: Copy

2
0
1
0
0

Description

thank @rushcheyo translation

This problem has a lot of practice. But I am currently being monolingual. Slowly update.

 

Solution a: dfs sequence + tree array.

Obtaining first sequences and dfs in each node the number of sub-tree, all the nodes and then by descending weight row. Descending insert Fenwick tree, inquiries about before inserting dfs Order Intervals and OK.

#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;

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 * 10 + ch - 48; ch = getchar(); }
    return x * f;
}

const int N = 1e5 + 10;
vector<int> G[N];
struct P {
    int u, val;
    bool operator < (const P &rhs) const {
        return val > rhs.val;
    }
} p[N];
int dfn[N], size[N], n, cnt, tree[N];

inline int lowbit(int x) { return x & -x; }

void add(int x) {
    for (int i = x; i <= n; tree[i]++, i += lowbit(i));
}

int query(int x) {
    int res = 0;
    for (int i = x; i; res += tree[i], i -= lowbit(i));
    return res; 
}

void dfs(int u, int fa) {
    size[u] = 1;
    dfn[u] = ++cnt;
    for (auto v : G[u]) {
        if (v == fa) continue;
        dfs(v, u);
        size[u] += size[v];
    }
}

int ans[N];

int main() {
    n = read();
    for (int i = 1; i <= n; i++) {
        p[i].val = read();
        p[i].u = i;
    }
    for (int i = 2; i <= n; i++) {
        int u = read();
        G[u].eb(i);
    }
    dfs(1, 1);
    sort(p + 1, p + n + 1);
    for (int i = 1; i <= n; i++) {
        int u = p[i].u;
        ans[u] = query(dfn[u] + size[u] - 1) - query(dfn[u]);
        add(dfn[u]); 
    }
    for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Mrzdtz220/p/11135900.html