QOJ.ac

QOJ

時間限制: 6 s 記憶體限制: 512 MB 總分: 100

#11278. 56TP

统计

Given a rooted tree with $n$ vertices, each vertex $i$ has a weight $v(t, i)$ at time $t$.

For each vertex, $v(0, i)$ is given. It is guaranteed that for non-leaf nodes $i$, $v(0, i) = 0$, and for leaf nodes $i$, $0 \le v(0, i) \le n$.

There are $m$ queries. Each query provides $x, y, t$, and asks for the minimum, maximum, and sum of the weights on the path from $x$ to $y$ at time $t$.

For leaf nodes $i$, $v(t, i) = v(0, i)$.

For non-leaf nodes $i$ and $t > 0$, $v(t, i)$ is the maximum of $v(t-1, j)$ for all children $j$ of $i$.

Input

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

The next $n-1$ lines each contain an integer, representing $f_2, \dots, f_n$ respectively, where $f_i$ is the parent of node $i$. The root is $1$.

The next $n$ lines each contain an integer, representing $v(0, 1), v(0, 2), \dots, v(0, n)$ respectively.

The next $m$ lines each contain three integers representing $x, y, t$.

Output

Output $m$ lines, each containing three integers representing the answer to each query.

Examples

Input 1

8 3
1
2
3
3
3
4
4
0
0
0
0
7
5
7
5
7 5 8
1 2 8
8 2 8

Output 1

7 7 28
7 7 14
5 7 26

Subtasks

Idea: nzhtl1477, Solution: ccz181078, Code: ccz181078, Data: ccz181078

For $100\%$ of the data, $1 \le n, m \le 10^6$, $1 \le f_i \le i-1$, $0 \le v(0, i) \le n$, $1 \le x, y, t \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.