Small fresh data structure that

First we have to find a property for every tree, we changed a root (the original root of a son \ (v_1 \) credited to the new root)

We remember this tree and weights for the sum, and weights for each sub-tree \ (S [i] \) , change root for each affected \ (S [i] \) only the root itself and \ (V_1 \) , and satisfies: \ (S [RT] - SUM> - S [V_1] \) , \ (S [V_1] -> S [RT] \)

So we can startling discovery: \ (\ SUM (SUM - S [i]) * S [i] \) is a fixed value! ! !

He apart: \ (SUM * \ S SUM [I] - \ S SUM [I] ^ 2 \)

And we ask what \ (\ SUM S [i] ^ 2 \) , so long as we maintain \ (sum * \ sum S [ i] \)

Because we will modify the weights found sum value well maintained, so we only need to maintain \ (\ sum S [i] \) to

We assumed that x is the root, then the \ (\ sum S [i] = \ sum (dep [i] + 1) * val [i] = sum + \ sum dep [i] * val [i] \)

If we do not re-evaluated dep, namely into a persimmon: $ \ sum S [i] = sum + \ sum val [i] * dis (i, x) $

This equation is the dynamic point of partition template, but we actually have a problem: that is what we value ( \ (\ SUM (SUM - S [i]) * S [i] \) ) to how demand it?

We also considered separately for each point: the meaning of the above persimmon tree actually find the child and all points * (total and sub-tree - the tree and ideas)

We considered separately for each point \ ((i, j) \ ) contributions, that is, two points are points in the plot of the number of different sets of weights *

In fact, this formula can then be converted to \ (\ sum_ {i = 1 } ^ n \ sum_ {j = i + 1} ^ nval [i] * val [j] * dis (i, j) \)

The \ (val [i] \) in advance, we obtain: \ (\ sum_ {I =. 1} ^ n-Val [I] * \ sum_ {J = I +. 1} ^ n-Val [J] * DIS (I, j) \)

Therefore, each modification (a [x] -> y ), that is, the amount of change \ ((y - a [x ]) * \ sum val [j] * dis (j, i) \)

Is not that \ (\ sum S [i] \) do? ? ?

Thus, we can get:
\ [\ SUM (SUM - S [I]) * S [I] = SUM * \ S SUM [I] - \ S SUM [I] ^ 2 \]

\[\sum S[i] ^ 2 = sum * \sum S[i] - \sum (sum - S[i]) * S[i]\]

\ (sum - 1 \) directly maintain, \ (\ sum S [i] \) used to maintain the dynamic point partition, we can later pile \ (\ sum S [i] \) is maintained, so we I finished it

Guess you like

Origin www.cnblogs.com/bcoier/p/11618363.html