#P4719. 【模板】动态 DP

    ID: 3595 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树O2优化树链剖分,树剖矩阵乘法

【模板】动态 DP

Description

Given a tree with nn nodes, each node has a weight.

There are mm operations. In each operation, you are given x,yx, y, which means changing the weight of node xx to yy.

After each operation, you need to output the total weight of the maximum weight independent set of this tree.

Input Format

The first line contains two integers, representing the number of nodes nn and the number of operations mm.

The second line contains nn integers. The ii-th integer is the weight aia_i of node ii.

In the next (n1)(n - 1) lines, each line contains two integers u,vu, v, indicating that there is an edge connecting uu and vv.

In the next mm lines, each line contains two integers x,yx, y, indicating an operation that changes the weight of node xx to yy.

Output Format

For each operation, output one line with one integer representing the answer.

10 10
-11 80 -99 -76 56 38 92 -51 -34 47 
2 1
3 1
4 3
5 2
6 2
7 1
8 2
9 4
10 7
9 -44
2 -17
2 98
7 -58
8 48
3 99
8 -61
9 76
9 14
10 93

186
186
190
145
189
288
244
320
258
304

Hint

Constraints

  • For 30%30\% of the testdata, n,m10n, m \le 10.
  • For 60%60\% of the testdata, n,m103n, m \le 10^3.
  • For 100%100\% of the testdata, 1n,m1051 \le n, m \le 10^5, 1u,v,xn1 \leq u, v, x \leq n, and 102ai,y102-10^2 \leq a_i, y \leq 10^2.

Translated by ChatGPT 5