PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000116. Đỉnh ấn tượng nhất (The Awesomest Vertex)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đỉnh ấn tượng nhất (The Awesomest Vertex)

The Awesomest Vertex

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a tree rooted at vertex 11, containing nn vertices numbered from 11 to nn. Vertex ii has two integers aia_i and bib_i.

Let R(v)R(v) consist of every vertex on the path from root 11 to vertex vv, including both endpoints. Define the awesomeness of vv by

$$F(v)=\left|\sum_{w\in R(v)}a_w\right|\cdot \left|\sum_{w\in R(v)}b_w\right|.$$

Process qq operations in order:

  • 1 v x: increase ava_v by the positive integer xx, leaving bvb_v unchanged.
  • 2 v: output the maximum awesomeness among all vertices in the subtree rooted at vv, including vv itself.

Updates affect all subsequent queries. Absolute values are applied to the two path sums, not separately to the individual vertex values.

Input

  • The first line contains integers n,qn,q.
  • The second line contains n−1n-1 integers p2,p3,…,pnp_2,p_3,\ldots,p_n, where pip_i is the parent of vertex ii. For n=1n=1, this line is empty.
  • The third line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.
  • The fourth line contains nn integers b1,b2,…,bnb_1,b_2,\ldots,b_n.
  • Each of the following qq lines describes an operation in one of the two formats above.

The condition pi<ip_i<i guarantees a tree rooted at 11.

Output

For each operation of type 2, print the maximum awesomeness in the requested subtree on its own line, in input order.

The answer may exceed the range of a 32-bit integer, so 64-bit integer arithmetic is required.

Subtasks

  • Subtask 1 (10 points): 1≤n≤501\le n\le50, 1≤q≤801\le q\le80; 1≤pi<i1\le p_i<i; initial −5000≤ai,bi≤5000-5000\le a_i,b_i\le5000; 1≤v≤n1\le v\le n, 1≤x≤50001\le x\le5000.
  • Subtask 2 (20 points): 1≤n≤6001\le n\le600, 1≤q≤10001\le q\le1000; 1≤pi<i1\le p_i<i; initial −5000≤ai,bi≤5000-5000\le a_i,b_i\le5000; 1≤v≤n1\le v\le n, 1≤x≤50001\le x\le5000.
  • Subtask 3 (30 points): 1≤n≤1041\le n\le10^4, 1≤q≤2⋅1041\le q\le2\cdot10^4; 1≤pi<i1\le p_i<i; initial −5000≤ai,bi≤5000-5000\le a_i,b_i\le5000; 1≤v≤n1\le v\le n, 1≤x≤50001\le x\le5000.
  • Subtask 4 (40 points): 1≤n≤2⋅1051\le n\le2\cdot10^5, 1≤q≤1051\le q\le10^5; 1≤pi<i1\le p_i<i; initial −5000≤ai,bi≤5000-5000\le a_i,b_i\le5000; type 1 v x has 1≤v≤n1\le v\le n, 1≤x≤50001\le x\le5000; type 2 v has 1≤v≤n1\le v\le n.

Examples

Input

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

Output

100
91
169
240

Explanation

Initially the pairs of root-path sums (∑a,∑b)(\sum a,\sum b) are (10,10)(10,10) at vertex 11, (7,13)(7,13) at vertex 22, (3,19)(3,19) at vertex 33, (4,16)(4,16) at vertex 44, and (−3,19)(-3,19) at vertex 55. Hence F=[100,91,57,64,57]F=[100,91,57,64,57]; operations 2 1 and 2 2 yield 100100 and 9191.

After 1 2 6, the aa-path sums at vertices 2,4,52,4,5 increase by 66; their awesomeness becomes 169,160,57169,160,57, so 2 1 yields 169169. After 1 2 5, vertex 44 has aa-path sum 1515 and bb-path sum 1616, giving F(4)=240F(4)=240, the maximum over the tree.