简化题意:一颗带有点权、以\(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\n", anss[i]);
return 0;
}
原文地址:https://www.cnblogs.com/morslin/p/11855688.html
时间: 2024-10-08 14:56:01