树状数组
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; const int maxn = 1000000 + 10; vector<int>Tree[50000 + 10]; int n; int val[50000 + 10]; int c[maxn]; int ans[50000 + 10]; int lowbit(int x) { return x & (-x); } void Update(int x, int add) { while (x < maxn) { c[x] += add; x += lowbit(x); } } int Get(int x) { int ret = 0; while (x != 0) { ret += c[x]; x -= lowbit(x); } return ret; } void init() { for (int i = 0; i <= n; i++) Tree[i].clear(); memset(c, 0, sizeof c); } void DFS(int now) { int A, B; A = Get(1000000 + 1) - Get(val[now]); for (int i = 0; i < Tree[now].size(); i++) DFS(Tree[now][i]); B = Get(1000000 + 1) - Get(val[now]); ans[now] = B - A; Update(val[now], 1); } int main() { while (~scanf("%d", &n)) { init(); for (int i = 1; i < n; i++) { int k; scanf("%d", &k); Tree[k].push_back(i); } for (int i = 0; i < n; i++) { scanf("%d", &val[i]); val[i]++; } DFS(0); for (int i = 0; i < n; i++) { if (i < n - 1) printf("%d ", ans[i]); else printf("%d ", ans[i]); } } return 0; }