403073: GYM100989 K Objects Panel (B)
Description
Since the list has a fixed height, Raihan had to implement a vertical scrollbar. In order for the scrollbar to work properly, the actual height of the items must be provided after every change in the state.
A change in the state means expanding or collapsing the items nested inside an object that is visible in the list.
Your task is to calculate the number of rows in the panel after each of Q given changes.
InputThe first line of input contains two integers N (1 ≤ N ≤ 3x105) and Q (1 ≤ Q ≤ 5x105), the number of objects and the number of changes, respectively.
The second line of input contains N integers, the ith (starting from 1) integer is the number of the parent object of object i. Note that the parent can be 0, and this means it is nested directly inside project.
The third line of input contains Q integers, each is an object number (from 0 to N) and represents a change in the state of an item, that is, a click over the sign beside that object.
Initially, all objects are expanded.
It is guaranteed that changes do not include hidden objects (objects that are not visible in the panel at the time of the change), or objects that don’t have any nested objects inside them.
OutputThe output should contain Q integers on a single line, the ith of them is the number of rows in the panel after toggling the state of the ith object.
ExamplesInput5 5Output
3 3 0 1 3
1 1 0 0 3
5 6 1 6 2Input
1 4Output
0
0 0 0 0
1 2 1 2