#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge
#ifdef DONLINE_JUDGE
// palindromic tree is better than splay tree!
#define lld I64d
#endif
int N, Pp =0;
vector<int> P,path,S;
struct fin {
vector<int> T;
fin() {}
fin(int N) {T.resize(N+10,0);}
int lastone(int x) {return x&(x^(x-1));}
void put(int pos) {
for(int i =pos+1; i < (int)T.size(); i +=lastone(i)) T[i]++;}
int get(int pos) {
int ret =0;
for(int i =pos+1; i > 0; i -=lastone(i)) ret +=T[i];
return ret;}
};
vector<fin> Fp;
vector< map<int,int> > Sp;
void prep(int R, vector< vector<int> > &son) {
int topson =-1;
ALL_THE(son[R],it) {
prep(*it,son);
S[R] +=S[*it];
if(topson == -1 || S[*it] > S[topson]) topson =*it;
}
if(topson == -1) {
path[R] =Pp;
Pp++;}
else path[R] =path[topson];
ALL_THE(son[R],it) if(*it != topson)
ALL_THE(Sp[path[*it]],jt) Sp[path[R]][jt->ff] =0;
Sp[path[R]][P[R]] =0;}
int count_brute(int R, vector< vector<int> > &son, int val, vector<int> &listP) {
int ans =0;
ALL_THE(son[R],it)
ans +=count_brute(*it,son,val,listP);
listP.push_back(P[R]);
if(P[R] > val) ans++;
return ans;}
vector<int> ans;
void count(int R, vector< vector<int> > &son) {
vector<int> listP;
ALL_THE(son[R],it) {
count(*it,son);
if(path[*it] != path[R]) ans[R] +=count_brute(*it,son,P[R],listP);
else ans[R] +=S[*it]-Fp[path[R]].get(Sp[path[R]][P[R]]);}
ALL_THE(listP,it) Fp[path[R]].put(Sp[path[R]][*it]);
Fp[path[R]].put(Sp[path[R]][P[R]]);}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
#ifndef LOCAL
freopen("promote.in","r",stdin);
freopen("promote.out","w",stdout);
#endif
int N;
cin >> N;
P.resize(N);
Fp.resize(N);
Sp.resize(N);
for(int i =0; i < N; i++) cin >> P[i];
path.resize(N,-1);
S.resize(N,1);
vector< vector<int> > son(N);
for(int i =1; i < N; i++) {
int p;
cin >> p;
son[p-1].push_back(i);}
prep(0,son);
for(int i =0; i < Pp; i++) {
int x =0;
ALL_THE(Sp[i],it) it->ss =x++;
Fp[i] =fin(x);}
ans.resize(N,0);
count(0,son);
for(int i =0; i < N; i++) cout << ans[i] << "\n";
return 0;}
// look at my code
// my code is amazing
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Ci8vIGlvc3RyZWFtIGlzIHRvbyBtYWluc3RyZWFtCiNpbmNsdWRlIDxjc3RkaW8+Ci8vIGJpdGNoIHBsZWFzZQojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxzdGFjaz4KI2luY2x1ZGUgPGxpc3Q+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDx0aW1lLmg+CiNkZWZpbmUgZGlicyByZXNlcnZlCiNkZWZpbmUgT1ZFUjkwMDAgMTIzNDU2Nzg5MAojZGVmaW5lIEFMTF9USEUoQ0FLRSxMSUUpIGZvcihhdXRvIExJRSA9Q0FLRS5iZWdpbigpOyBMSUUgIT0gQ0FLRS5lbmQoKTsgTElFKyspCi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