QOJ.ac

QOJ

Time Limit: 5 s Memory Limit: 512 MB Total points: 100

#18822. Tree and Query 19

Statistics

There is a tree (an undirected connected graph with no cycles) consisting of $N$ vertices. The vertices are numbered from $1$ to $N$, and each vertex is colored either black or white.

Write a program that processes the following query:

  • $X$: Toggle the color of vertex $X$ (white $\to$ black, black $\to$ white). After that, output the sum of the LCA (lowest common ancestor) levels over all distinct pairs of white vertices $(a, b)$.

The root vertex is vertex $1$. The level of the root is $0$, and the level of any other vertex is defined as (level of its parent) $+ 1$.

Input

The first line contains two integers $N$ and $Q$, the number of vertices and the number of queries.

The second line contains $N$ integers $t_1, t_2, \cdots, t_N$ representing the colors of vertices $1, 2, \cdots, N$. For each $1 \le i \le N$, $t_i = 0$ means black and $t_i = 1$ means white.

The third line contains $N-1$ integers $P_2, P_3, \cdots, P_N$, representing the parent of vertices $2, 3, \cdots, N$.

The next $Q$ lines each contain an integer $X$ representing a query.

Output

The first line outputs the answer for the initial state.

The next $Q$ lines each output the answer for the corresponding query.

Examples

Input 1

5 5
1 0 0 1 1
1 1 3 2
5
3
5
4
2

Output 1

0
0
1
1
0
1

Input 2

10 10
1 0 0 1 1 0 1 0 1 0
1 2 1 4 5 6 1 4 1
8
4
4
4
10
9
2
1
5
3

Output 2

7
7
4
7
4
4
2
2
2
0
1

Input 3

20 20
1 0 1 0 1 0 0 1 1 0 0 0 0 1 0 0 0 1 1 0
1 2 3 4 2 2 1 3 2 4 4 3 3 3 8 14 11 7 7
5
12
19
5
10
18
17
13
10
5
5
13
10
4
11
8
14
13
19
15

Output 3

26
16
26
21
33
39
26
38
51
44
31
44
32
38
53
71
71
55
70
79
97

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.