#4628. [UTS 2024] Remove Node

    ID: 4628 传统题 2500ms 1024MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>P12563线段树树状数组2024UOI(乌克兰)

[UTS 2024] Remove Node

[UTS 2024] Remove Node

题目描述

You have a rooted tree with $n$ vertices, each assigned a value $a_x$. The root is node $1$.

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 ∣ax−ay∣|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 $n$ $(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 (1≤ai≤109)(1 \le a_i \le 10^9), representing the assigned values of each node.

The third line contains n−1n-1 integers p2,p3,…,pnp_2, p_3, \dots, p_n (1≤pi≤n1 \leq p_i \leq n), representing the parent of each node.

The fourth line contains one integer qq (1≤q≤200 000)(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 (1≤x≤n1 \leq x \leq n, 1≤y≤1091\leq y \leq 10^9), or 2 x2\ x (1≤x≤n1\leq x \leq n).

输出格式

On each line of the input, you need to print the result of a query of type $2$, 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

提示

- ($4$ points) $n \le 1000, q=1$; - ($13$ points) $q=1$; - ($15$ points) the tree is a chain and there are only queries of the second type; - ($24$ points) there are only queries of the second type; - ($12$ points) $p_i=1$; - ($32$ points) no additional constraints. 标签: P12563|线段树|树状数组|2024|UOI(乌克兰)

来源

P12563|[UTS 2024] Remove Node