PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000110. Kỳ nghỉ tháng Năm (May Holidays)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Kỳ nghỉ tháng Năm (May Holidays)

May Holidays

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A company has nn employees, numbered from 11 to nn. Employee 11 is the head manager. For every employee i≥2i\ge2, employee pip_i is their unique direct manager. The management relation forms a rooted tree with root 11: repeatedly following parent links from any employee eventually reaches employee 11.

Employee vv is a subordinate of uu if following one or more parent links from vv reaches uu. Thus an employee is not their own subordinate. Let sus_u be the number of subordinates of employee uu.

Each employee uu has a tolerance tut_u between 00 and sus_u. Employee uu is displeased on a given day if and only if both conditions hold:

  1. Employee uu is not on vacation.
  2. The number of subordinates of uu currently on vacation is strictly greater than tut_u.

There are mm days, with exactly one event at the beginning of each day, given by a nonzero integer qdq_d:

  • If qd>0q_d>0, employee qdq_d starts a vacation on that day.
  • If qd<0q_d<0, employee −qd-q_d returns to work on that day.

Initially nobody is on vacation. All events are valid: an employee already on vacation cannot start another vacation, and a working employee cannot return from vacation.

Task: After each event, count the number of displeased employees. An employee currently on vacation is never counted as displeased, regardless of how many of their subordinates are absent.

Input

  • Line 1: two integers n,mn,m.
  • Line 2: n−1n-1 integers p2,p3,…,pnp_2,p_3,\ldots,p_n giving direct managers (pi<ip_i<i is not required).
  • Line 3: nn integers t1,t2,…,tnt_1,t_2,\ldots,t_n.
  • Line 4: mm integers q1,q2,…,qmq_1,q_2,\ldots,q_m, processed in their given order.

Output

Print mm integers a1,a2,…,ama_1,a_2,\ldots,a_m in day order, each equal to the number of displeased employees after that day's event. Separate the integers by spaces or newlines.

Subtasks

  • Subtask 1 (15 points): 2≤n≤602\le n\le 60; 2≤m≤602\le m\le 60; 1≤pi≤n1\le p_i\le n for every 2≤i≤n2\le i\le n, the edges (pi,i)(p_i,i) form a tree rooted at 11; 0≤ti≤si0\le t_i\le s_i where sis_i is the number of proper subordinates; 1≤∣qd∣≤n1\le |q_d|\le n for every 1≤d≤m1\le d\le m; vacation/return events are valid and initially nobody is absent.
  • Subtask 2 (20 points): 2≤n≤2 0002\le n\le 2\,000; 2≤m≤2 0002\le m\le 2\,000; 1≤pi≤n1\le p_i\le n for every 2≤i≤n2\le i\le n, the edges (pi,i)(p_i,i) form a tree rooted at 11; 0≤ti≤si0\le t_i\le s_i where sis_i is the number of proper subordinates; 1≤∣qd∣≤n1\le |q_d|\le n for every 1≤d≤m1\le d\le m; vacation/return events are valid and initially nobody is absent.
  • Subtask 3 (25 points): 2≤n≤20 0002\le n\le 20\,000; 2≤m≤20 0002\le m\le 20\,000; 1≤pi≤n1\le p_i\le n for every 2≤i≤n2\le i\le n, the edges (pi,i)(p_i,i) form a tree rooted at 11; 0≤ti≤si0\le t_i\le s_i where sis_i is the number of proper subordinates; 1≤∣qd∣≤n1\le |q_d|\le n for every 1≤d≤m1\le d\le m; vacation/return events are valid and initially nobody is absent.
  • Subtask 4 (40 points): 2≤n≤100 0002\le n\le 100\,000; 2≤m≤100 0002\le m\le 100\,000; 1≤pi≤n1\le p_i\le n for every 2≤i≤n2\le i\le n, the edges (pi,i)(p_i,i) form a tree rooted at 11; 0≤ti≤si0\le t_i\le s_i where sis_i is the number of proper subordinates; 1≤∣qd∣≤n1\le |q_d|\le n for every 1≤d≤m1\le d\le m; vacation/return events are valid and initially nobody is absent.

Examples

Example 1

Input

7 8
4 5 1 1 5 5
0 0 0 1 2 0 0
2 6 3 7 -2 4 -3 1

Output

1 1 1 2 2 2 1 0

Explanation

The tree has root 11, with p2=4p_2=4, p3=5p_3=5, p4=p5=1p_4=p_5=1 and p6=p7=5p_6=p_7=5. On day 1, employee 22 goes on vacation, so employee 11 (whose tolerance is 00) becomes displeased. After the next two days, the count is still 11. On day 4, employee 77 goes on vacation, leaving employee 55 with three absent subordinates, more than tolerance t5=2t_5=2, so the answer becomes 22. Day 5 brings employee 22 back but the head manager is still displeased. Day 6 sends employee 44 on vacation; employee 44 cannot be counted while absent. Day 7 returns employee 33, reducing employee 55's absent subordinates to two, so the answer becomes 11. Day 8 sends employee 11 on vacation, leaving no displeased employees.

Example 2

Input

5 6
1 2 3 4
4 0 0 1 0
1 5 2 3 -5 -1

Output

0 2 1 0 0 0

Explanation

The hierarchy is a chain 1→2→3→4→51\to2\to3\to4\to5. Employee 11 has tolerance 44, so their absent-subordinate count cannot exceed this limit. On day 1, employee 11 goes on vacation; the answer is 00. On day 2, employee 55 goes on vacation, causing working employees 22 and 33 to exceed their zero tolerance and giving answer 22. On day 3, employee 22 also takes leave, so they can no longer be counted and the result is 11. On day 4, employee 33 goes on leave, and no remaining working employee exceeds their tolerance; the answer is 00. The final two returns do not make anyone displeased.