luogu#P4751. 【模板】动态 DP(加强版)

    ID: 3742 远端评测题 3500ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP线段树O2优化树链剖分动态树 LCT动态 DP全局平衡二叉树

【模板】动态 DP(加强版)

Background

Tree decomposition has small constants! It cannot reach full performance!

shadowice1984 made this nasty problem to prove to you that he can hack tree decomposition and knows how to do it.

It is guaranteed that all answers fit in the int range.

Then it got targeted by offline algorithms...

So this problem becomes forced online.

Problem Description

Same as P4719.

Given a tree with nn nodes and node weights, perform mm operations that modify node weights.

After each modification, you need to output the maximum total weight of a maximum weight independent set on the tree.

Input Format

Same as P4719.

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

The second line contains nn integers V1,,VnV_1,\dots,V_n, representing the weight of each node.

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

The next mm lines each contain two integers xx, yy, indicating that the weight of node xx is modified to yy.

For the 1st operation, xx is the index of the modified node.

For the 2nd to the mm-th operations, the index of the modified node is xlastansx\oplus lastans.

Here, lastanslastans is the answer output after the previous operation, and \oplus denotes the bitwise XOR operation.

Output Format

Output mm lines. The ii-th line is the total weight of the maximum weight independent set on the tree after the ii-th operation.

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
184 -17
184 98
185 -58
153 48
190 99
296 -61
253 76
329 14
264 93

186
186
190
145
189
288
244
320
258
304

Hint

Constraints: n1×106n \leq 1 \times 10^6, m3×106m \leq 3 \times 10^6. It is guaranteed that at any time, the absolute value of each node weight is 100\leq 100.

The time limit is twice that of the reference solution. If you need to optimize constants, please use the int type.

Translated by ChatGPT 5