QOJ.ac

QOJ

حد الوقت: 5 s حد الذاكرة: 512 MB مجموع النقاط: 100

#18822. 트리와 쿼리 19

الإحصائيات

$N$개의 정점으로 이루어진 트리(무방향 사이클이 없는 연결 그래프)가 있다. 정점은 $1$번부터 $N$번까지 번호가 매겨져 있고, 모든 정점의 색은 검정색 또는 흰색이다.

아래의 쿼리를 처리하는 프로그램을 작성하시오.

  • $X$ : $X$번 정점의 색을 바꾼다. (흰색 $\to$ 검정색, 검정색 $\to$ 흰색) 이후 서로 다른 모든 흰색 정점 쌍 $(a,b)$에 대해 LCA(최소 공통 조상)레벨의 합을 출력한다.

루트 정점은 $1$번 정점이다. 루트 정점의 레벨은 $0$이며, 다른 정점의 레벨은 (부모 정점의 레벨) $+ 1$로 정의한다.

입력

첫째 줄에 정점의 갯수 $N$과 쿼리의 갯수 $Q$가 주어진다.

뚤째 줄에는 $1, 2, \cdots, N$번 정점의 색을 나타내는 정수 $t_1, t_2, \cdots, t_N$이 주어진다. $1 \le i \le N$인 자연수 $i$에 대해 $t_i = 0$인 경우 검정색이고 $t_i = 1$인 경우 흰색이다.

셋째 줄에는 $2, 3, \cdots, N$번 정점의 부모 정점을 나타내는 자연수 $P_2, P_3, \cdots, P_N$이 주어진다.

다음 $Q$개의 줄에는 쿼리를 나타내는 $X$가 주어진다.

출력

첫번째 줄에는 초기 상태의 정답을 출력한다.

두번째 줄부터 $Q$개의 줄에 걸쳐 각 쿼리의 정답을 출력한다.

Sample

Input

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

Output

0
0
1
1
0
1

Sample

Input

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

7
7
4
7
4
4
2
2
2
0
1

Sample

Input

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

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.