QOJ.ac

QOJ

実行時間制限: 3 s メモリ制限: 2048 MB 満点: 100

#7446. spxmcq

統計

Given a rooted tree with $n$ nodes, where the $i$-th node has a weight $a_i$.

The tree supports the following type of query:

  • Given a node $u$ and a parameter $x$, suppose the weights of all nodes are increased by $x$. In this case, find: the maximum possible sum of weights for all connected subgraphs that are entirely contained within the subtree of $u$ and include $u$.

Input

The first line contains two positive integers $n$ and $m$.

The second line contains $n-1$ positive integers $f_2, f_3, \dots, f_n$, which are the parent node indices of $2, 3, \dots, n$ respectively, where it is guaranteed that $1 \le f_i < i$.

The third line contains $n$ integers $a_1, a_2, \dots, a_n$, which are the weights of nodes $1, 2, \dots, n$ respectively.

The next $m$ lines each contain a positive integer $u$ and an integer $x$, representing a query, where it is guaranteed that $1 \le u \le n$.

Output

Output $m$ lines, each containing an integer representing the answer to the corresponding query.

Examples

Input 1

10 6
1 1 2 2 3 5 5 5 6
5 2 3 1 -5 -7 1 1 1 2
1 0
1 -2
1 3
2 1
5 0
5 -2

Output 1

11
4
34
7
-2
-7

Subtasks

Idea: w33z8kqrqk8zzzx33, Solution: w33z8kqrqk8zzzx33 & ccz181078, Code: w33z8kqrqk8zzzx33, Data: w33z8kqrqk8zzzx33

For $100\%$ of the data, $1 \le n, m \le 10^6$, $|a_i|, |x| \le 10^8$, and $1 \le u \le n$.

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.