inlineintread() { char ch = getchar(); int num = 0; bool flag = false; while (ch < '0' || ch > '9') { if (ch == '-') flag = true; ch = getchar(); } while (ch >= '0' && ch <= '9') { num = num * 10 + ch - '0'; ch = getchar(); } return flag ? -num : num; } /*----OI--------------*/ structseg { int l, r, sum; } tr[N * 20]; int scnt; voidbuild(int &pos, int l, int r, int v) { if (!pos) pos = ++scnt; tr[pos].sum++; if (l == r) return; int mid = (l + r) >> 1; if (v <= mid) build(tr[pos].l, l, mid, v); else build(tr[pos].r, mid + 1, r, v); } intquery(int pos, int l, int r, int w) { if (!pos) return0; if (w <= l) return tr[pos].sum;
int mid = (l + r) >> 1; if (w <= mid) return query(tr[pos].l, l, mid, w) + query(tr[pos].r, mid + 1, r, w); else return query(tr[pos].r, mid + 1, r, w); } intmerge(int u, int v) { if (!u || !v) return u + v; int pos = ++scnt; tr[pos].sum = tr[u].sum + tr[v].sum; tr[pos].l = merge(tr[u].l, tr[v].l); tr[pos].r = merge(tr[u].r, tr[v].r); return pos; } vector<int> g[N]; int num, li[N], a[N]; int rt[N], ans[N]; voiddfs(int x) { // cout << x << "-----" << endl; for (int to : g[x]) { dfs(to); rt[x] = merge(rt[x], rt[to]); } //cout << "----" << endl; ans[x] = query(rt[x], 1, num, a[x] + 1); build(rt[x], 1, num, a[x]); }
intmain() { int n = read(); for (int i = 1; i <= n; i++) a[i] = read(), li[++num] = a[i]; sort(li + 1, li + 1 + num); num = unique(li + 1, li + 1 + num) - li - 1; for (int i = 1; i <= n; i++) a[i] = lower_bound(li + 1, li + 1 + num, a[i]) - li; for (int i = 2; i <= n; i++) { int fa = read(); g[fa].push_back(i); } dfs(1); for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]); }