#P12563. [UTS 2024] Remove Node

    ID: 14130 远端评测题 2500ms 1024MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>线段树树状数组2024UOI(乌克兰)

[UTS 2024] Remove Node

题目描述

You have a rooted tree with nn vertices, each assigned a value axa_x. The root is node 11.

You can perform operations where you merge two adjacent nodes xx and yy into a single node zz, with the value of zz being equal to the minimum value between the two nodes. This operation changes edges connected to xx or yy to be connected to zz. The cost of this operation is axay|a_x - a_y|. The total cost of multiple operations is the sum of individual operation costs.

For queries, you have two types:

  • 1 x y1\ x\ y: This updates the value of node xx to yy.
  • 2 x2\ x: This asks for the minimum cost of operations to reduce the subtree rooted at xx to just one node.

输入格式

The first line contains a single integer nn (1n200000)(1 \le n \le 200\,000), the number of vertices.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai109)(1 \le a_i \le 10^9), representing the assigned values of each node.

The third line contains n1n-1 integers p2,p3,,pnp_2, p_3, \dots, p_n (1pin1 \leq p_i \leq n), representing the parent of each node.

The fourth line contains one integer qq (1q200000)(1 \le q \le 200\,000), representing the total number of queries.

The following qq lines each contain a query:

Either 1 x y1\ x\ y (1xn1 \leq x \leq n, 1y1091\leq y \leq 10^9), or 2 x2\ x (1xn1\leq x \leq n).

输出格式

On each line of the input, you need to print the result of a query of type 22, in the same order as the order from the input.

7
4 7 9 7 4 1 2
1 1 3 2 3 2
1
2 1
11
7
6 6 5 1 6 6 4
1 1 2 3 3 3
3
2 1
1 1 1
2 1
7
11

提示

  • (44 points) n1000,q=1n \le 1000, q=1;
  • (1313 points) q=1q=1;
  • (1515 points) the tree is a chain and there are only queries of the second type;
  • (2424 points) there are only queries of the second type;
  • (1212 points) pi=1p_i=1;
  • (3232 points) no additional constraints.