USACO17JAN Promotion Counting

Posted morslin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了USACO17JAN Promotion Counting相关的知识,希望对你有一定的参考价值。

题目传送门


简化题意:一颗带有点权、以(1)为根的树,对于每个节点(x),求出(x)的子树中有多少个点满足该点的点权大于(x)的点权

先将点权离散化
对这棵树进行DFS,在DFS到(x)时,加入该点点权,然后在DFS它的子树前记录一下当前有多少节点大于(x),记为(last)。在回溯到该节点时再次查询当前有多少节点大于(x),减去(last)即为答案

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define LL long long
#define lb (i & -i)
using namespace std;
LL read() {
    LL k = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9')
        k = k * 10 + c - 48, c = getchar();
    return k * f;
}
struct zzz {
    int t, nex;
}e[100010 << 1]; int head[100010], tot;
void add(int x, int y) {
    e[++tot].t = y;
    e[tot].nex = head[x];
    head[x] = tot;
}
int size[100010], dfn[100010], num;
int v[100010], b[100010];
int tree[100010 << 1], n;
void update(int x, int k) {
    for(int i = x; i <= n; i += lb)
        tree[i] += k;
}
int sum(int x) {
    int ans = 0;
    for(int i = x; i; i -= lb)
        ans += tree[i];
    return ans;
}
int anss[100010];
void dfs(int x, int fa) {
    update(v[x], 1);
    int last = sum(n) - sum(v[x]);
    for(int i = head[x]; i; i = e[i].nex) {
        if(e[i].t == fa) continue;
        dfs(e[i].t, x);
    }
    anss[x] += sum(n) - sum(v[x]) - last;
    
}
int main() {
    n = read();
    for(int i = 1; i <= n; ++i) v[i] = b[i] = read();
    sort(b+1, b+n+1); int cnt = unique(b+1, b+n+1) - b - 1;
    for(int i = 1; i <= n; ++i)
        v[i] = lower_bound(b+1, b+cnt+1, v[i]) - b; //cout << v[i] << endl;
    for(int i = 2; i <= n; ++i) {
        int x = read(); add(i, x); add(x, i);
    }
    dfs(1, 0);
    for(int i = 1; i <= n; ++i) printf("%d
", anss[i]);
    return 0;
}

以上是关于USACO17JAN Promotion Counting的主要内容,如果未能解决你的问题,请参考以下文章

题解晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

[USACO17JAN]Promotion Counting晋升者计数

P3605 [USACO17JAN]Promotion Counting P (dsu+线段树)

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

P3605 [USACO17JAN]Promotion Counting P (线段树合并)

P3605 [USACO17JAN]Promotion Counting P (线段树合并)