QOJ.ac

QOJ

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

#6735. 트리

Statistics

$n$개의 정점으로 이루어진 트리가 주어집니다. $i$번째 정점의 색은 $a_i$로 나타내며, $i$번째 간선은 $fa_i$번째 정점과 $(i+1)$번째 정점을 연결합니다. 이 간선의 색은 $fc_i$로, 길이는 $fw_i$로 나타냅니다.

단순 경로 위의 모든 정점이 같은 색이고 경로 위의 모든 간선도 같은 색일 때, 그리고 그때만 이 경로를 좋은 경로라고 정의합니다. 정점의 색과 간선의 색은 달라도 된다는 점에 유의하세요.

$q$번의 연산을 수행합니다. $i$번째 연산에서는 정점 $a_{x_i}$의 색을 $c_i$로 바꿉니다.

처음과 각 연산 후에 좋은 경로의 최대 길이를 구해야 합니다.

입력

입력의 첫 번째 줄에는 두 양의 정수 $n,q$ ($1\le n,q\le 2\times 10^5$)가 포함됩니다.

다음 줄에는 $n$개의 정수 $a_1,\cdots,a_n$ ($1\le a_i\le n$)이 포함됩니다.

다음 줄에는 $n-1$개의 정수 $fa_2,\cdots,fa_n$ ($1\le fa_i<i$)이 포함됩니다.

다음 줄에는 $n-1$개의 정수 $fc_2,\cdots,fc_n$ ($1\le fc_i\le n$)이 포함됩니다.

다음 줄에는 $n-1$개의 정수 $fw_2,\cdots,fw_n$ ($0\le fw_i\le 10^9$)이 포함됩니다.

다음 $q$개의 줄 중 $i$번째 줄에는 두 정수 $x_i$, $c_i$ ($1\le x_i,c_i\le n$)가 포함됩니다.

출력

$q+1$개의 줄을 출력해야 합니다.

출력의 첫 번째 줄에는 모든 질의 이전의 좋은 경로의 최대 길이를 나타내는 정수 하나를 출력하세요.

그런 다음 각 질의마다 해당 질의 이후의 좋은 경로의 최대 길이를 나타내는 정수 하나를 한 줄에 출력하세요.

예제

입력 1

5 5
5 4 3 4 5
1 2 3 1
2 2 2 2
4 9 2 6
2 5
4 5
5 4
3 5
2 1

출력 1

6
10
10
4
15
2

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#3213EditorialOpenNew Editorial for Problem #6735nullptr_qwqView
#153EditorialOpen题解jianglyView

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.