#P10147. [Ynoi1999] 56TP

[Ynoi1999] 56TP

Background

Problem Description

You are given a rooted tree with nn vertices. Each vertex ii has a weight v(t,i)v(t,i) at time tt.

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

There are mm queries. Each query gives x,y,tx,y,t, asking for the minimum value, maximum value, and sum of the weights at time tt along the path from xx to yy.

For a leaf node ii, v(t,i)=v(0,i)v(t,i)=v(0,i).

For a non-leaf node ii with t>0t>0, v(t,i)v(t,i) is the maximum of v(t1,j)v(t-1,j) over all children jj of ii.

Input Format

The first line contains two integers n,mn,m.

The next n1n-1 lines each contain one integer, in order, representing f2,,fnf_2,\dots,f_n, where fif_i denotes the parent of node ii, and the root is 11.

The next nn lines each contain one integer, in order, representing v(0,1),v(0,2),,v(0,n)v(0,1),v(0,2),\dots,v(0,n).

The next mm lines each contain three integers representing x,y,tx,y,t.

Output Format

Output mm lines. Each line contains one integer, which is the answer to the corresponding query.

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
7 7 28
7 7 14
5 7 26

Hint

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

For 100%100\% of the testdata, 1n,m1061\le n,m\le 10^6, 1fii11\le f_i\le i-1, 0v(0,i)n0\le v(0,i)\le n, 1x,y,tn1\le x,y,t\le n.

Translated by ChatGPT 5