403073: GYM100989 K Objects Panel (B)

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

K. Objects Panel (B)time limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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.

Input

The 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.

Output

The 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.

ExamplesInput
5 5
3 3 0 1 3
1 1 0 0 3
Output
5 6 1 6 2 
Input
1 4
0
0 0 0 0
Output
1 2 1 2 

加入题单

算法标签: