#include #include #include #include #include using namespace std; typedef long long ll; struct Treap { struct tr { int under; ll val; int pr; tr * L, * R; tr(): under(1), val(0), pr(rand()), L(0), R(0){} tr(ll val): under(1), val(val), pr(rand()), L(0), R(0) {} }; tr * root = 0; int below(tr *& now) { if(now == 0) return 0; else return now->under; } void rotRight(tr *& now) { tr * L = now->L; tr * a = L->R; now->L = a; L->R = now; now = L; } void rotLeft(tr *& now) { tr * R = now->R; tr * a = R->L; now->R = a; R->L = now; now = R; } void refresh(tr *& now) { if(now == 0) return; now->under = 1; now->under += below(now->L); now->under += below(now->R); } void rebalance(tr *& now) { if(now->L && now->L->pr > now->pr) rotRight(now); else if(now->R && now->R->pr > now->pr) rotLeft(now); refresh(now->L); refresh(now->R); refresh(now); } void insert(tr *& now, ll &val) { if(now == 0) { now = new tr(val); } else { if(val >= now->val) insert(now->R, val); else if(val < now->val) insert(now->L, val); } rebalance(now); } void insert(ll val) { insert(root, val); } ll query(tr *& now, ll &val) { if(now == 0) return 0; if(val < now->val) return query(now->L, val); else return 1 + below(now->L) + query(now->R, val); } ll query(ll val) { return query(root, val); } }; Treap T; const int MAX_N = 1e5 + 10; struct edge { int to; int next; }; edge E[MAX_N]; int at; int fst[MAX_N]; int TT[MAX_N]; ll lo[MAX_N]; ll hi[MAX_N]; void addEdge(int f, int to) { ++at; E[at].to = to; E[at].next = fst[f]; fst[f] = at; } int n; vector below[MAX_N]; void DFS(const int node) { const int beg = T.query(hi[TT[node]]); T.insert(lo[node]); for(int i = fst[node] ; i ; i = E[i].next) { const int son = E[i].to; DFS(son); } const int fin = T.query(hi[TT[node]]); below[TT[node]].push_back(fin - beg); } int main() { srand(0); cin >> n; for(int i = 2 ; i <= n ; ++i) { cin >> TT[i]; addEdge(TT[i], i); } for(int i = 1 ; i <= n ; ++i) cin >> lo[i]; for(int i = 1 ; i <= n ; ++i) cin >> hi[i]; DFS(1); for(int i = 1 ; i <= n ; ++i) { ll tot = 0; for(auto x : below[i]) tot += x; ll ans = 0; if(hi[i] >= lo[i]) { ans++; ans += 2 * tot; } for(auto x : below[i]) { tot -= x; ans += 1LL * x * tot; tot += x; } cout << ans << "\n"; } return 0; }