#P8479. 「GLR-R3」谷雨

「GLR-R3」谷雨

Background

  “A few new leaves on rustling bamboo, a few strokes of light texture on pale mountains.”


  That road from more than ten days ago was still fine, with two people together.

  “We’re very lucky,” A-Ling quietly took a sip of Tianyi, who had just stopped crying and started smiling, “May heaven bless us to be in a ……”

  Puffing her cheeks and pinching that soft waist, Tianyi could not help but smile as well. They were very lucky, just in time to make it in……

  At the end of rows upon rows, the sky looked like mountains rising and falling under clouds, rubbed with a light cyan ink-like joy.

  Their story continues, just as the crops are growing today.


  Grain Rain “I climbed over a high mountain, yet ahead there is still a long, long mountain road.”

Problem Description

Old V held a small party for everyone who performed well. To liven up the atmosphere, and also to carry out the idea of safety and environmental protection, (mainly because he couldn’t come up with anything else,) Old V brought a fancy “electronic firework,” calling it “firework tree and silver flowers.”

As its name suggests, this is a tree with nn nodes. Node uu has weight lul_u, representing the brilliance of the firework style set at that node. Everyone, out of curiosity, performed a total of qq operations on it. Let the nodes on the path from uu to vv in the tree (including u,vu,v) form the set P(u,v)P(u,v). Each operation is one of the following:

  1. Given node indices u,vu,v and a new brilliance kk, set the brilliance lwl_w to kk for every node ww such that wP(u,v)w\in P(u,v), or ww has an adjacent node P(u,v)\in P(u,v).

  2. Given u,vu,v, light the most “dazzling” subsegment of this string of fireworks. Specifically, maintain a sequence SS. Starting from uu, walk along tree edges toward vv. When you reach a node ww (ww may be uu or vv):

    • Append lwl_w to the end of sequence SS.
    • Enumerate ww’s adjacent nodes xx in increasing order of index. If xP(u,v)x\notin P(u,v), append lxl_x to the end of SS.
    • Finally, move to the next node.

After obtaining the final SS, the system will automatically light the subsegment of SS with the maximum sum of brilliance. The subsegment may be empty. You need to output this maximum value, i.e., for each operation 1., compute the maximum subarray sum allowing emptiness of SS.

Input Format

The first line contains an integer TT, indicating the subtask ID to which this testdata belongs.

The second line contains an integer nn, the number of nodes in the tree.

The third line contains nn integers. The ii-th integer lil_i is the initial weight of node ii.

The fourth line contains n1n-1 integers. To make it easier for contestants to process the data, assume node 11 is the root of the tree. The ii-th integer pip_i is the parent of node i+1i+1, describing an edge (pi,i+1)(p_i,i+1). It is guaranteed that pi<i+1p_i<i+1.

The fifth line contains an integer qq, the number of operations to process.

The next qq lines each have the format 0 u v k0~u~v~k or 1 u v1~u~v, corresponding to the two operations described in “Description.”

Output Format

Output several lines. The ii-th line should contain an integer aia_i, the answer to the ii-th operation of type 1.

0
5
1 2 3 4 5
1 2 2 1
5
1 1 2
0 2 3 -2
1 3 4
0 4 4 1
1 3 4

15
0
1

Hint

Explanation for Sample #1

This sample is not part of the testdata, so the first line uses T=0T=0 instead.

The 1st operation is a query. The visited nodes in order are 1,5,2,3,4\lang 1,5,2,3,4\rang, and the corresponding weight sequence is S=1,5,2,3,4S=\lang 1,5,2,3,4\rang. The maximum subarray sum is 1515.

The 2nd operation is an update, setting the node weights of nodes 2,1,4,32,1,4,3 to 2-2.

The 3rd operation is a query. The visited nodes in order are 3,2,1,4\lang 3,2,1,4\rang, and the corresponding weight sequence is S=2,2,2,2S=\lang -2,-2,-2,-2\rang. Note that the subsegment may be empty, so the maximum subarray sum is 00.

The 4th operation is an update, setting the node weights of nodes 4,24,2 to 11.

The 5th operation is a query. The visited nodes in order are 3,2,1,4\lang 3,2,1,4\rang, and the corresponding weight sequence is S=2,1,2,1S=\lang -2,1,-2,1\rang. The maximum subarray sum is 11.

Constraints

This problem uses Subtask-based scoring.

Let VV be the value range of the initial node weights and the node weights in update operations.

For 100%100\% of the data, 1n,q1051\le n,q\le10^5, 1pii1\le p_i\le i, V[109,109]V\subseteq[-10^9,10^9], and operation parameters satisfy 1u,vn1\le u,v\le n.

For different subtasks, the constraints are as follows:

Subtask ID n,qn,q VV Special Property Score
11 103\le10^3 [109,109]\subseteq[-10^9,10^9] None 1010
22 105\le10^5 A
33 B
44 C 1515
55 D
66 [0,109]\subseteq[0,10^9] None 1010
77 [109,109]\subseteq[-10^9,10^9] E 2020
88 None 1010
  • Special Property A: For all i[1,n)i\in[1,n), pi=ip_i=i holds.
  • Special Property B: For all operation parameters u,vu,v, u=vu=v holds.
  • Special Property C: There are no update operations.
  • Special Property D: Only the qq-th operation is a query operation.
  • Special Property E: For all parameters u,vu,v in query operations, when node 11 is the root, either u=vu=v or uu is an ancestor of vv.
  • Note: TT in the input only indicates the subtask ID of this data point. This data point may satisfy the constraints of other subtasks as well.

Translated by ChatGPT 5