QOJ.ac

QOJ

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

#6735. Tree

Statistics

You are given a tree with $n$ vertices. The $i$-th vertex has a color denoted by $a_i$, and the $i$-th edge connects the $fa_i$-th vertex with the $(i+1)$-th vertex. This edge has a color represented by $fc_i$ and a length indicated by $fw_i$.

A simple path is defined as good if and only if all vertices on the path share the same color and all edges along the path also share a common color. Note that the color of the vertices and the color of the edges can be different.

There are $q$ operations to be performed. In the $i$-th operation, the color of the vertex $a_{x_i}$ is changed to $c_i$.

At the beginning, and after each operation, you need to determine the maximum length of a good path.

Input

The first line of the input contain two positive integers $n,q$ ($1\le n,q\le 2\times 10^5$).

The next line contains $n$ integers $a_1,\cdots,a_n$ ($1\le a_i\le n$).

The next line contains $n-1$ integers $fa_2,\cdots,fa_n$ ($1\le fa_i<i$).

The next line contains $n-1$ integers $fc_2,\cdots,fc_n$ ($1\le fc_i\le n$).

The next line contains $n-1$ integers $fw_2,\cdots,fw_n$ ($0\le fw_i\le 10^9$).

The $i$-th of the next $q$ lines contains two integers $x_i$ and $c_i$ ($1\le x_i,c_i\le n$).

Output

You need to output $q+1$ lines.

The first line of the output contains a single integer, indicating the maximum length of a good path before all the queries.

Then, for each query, output a single line contains a single integer, indicating the maximum length of a good path after the query.

Examples

Input 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

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