Differential prefix

Differential prefix

Define a sequence $ A $, which difference sequence $ b_n = a_n-a_ {n -1} $ ( backward difference), prefix and sequence $ b_n = b_ {n-1 } + a_n $, obviously, the difference prefix and are inverse operations , which means that the differential sequence prefix and is seeking original sequence; Similarly, determining the difference of the original prefix and the sequence is a sequence.

Important conclusions

1, if they have the prefix sequence, we can O (1) obtained $ [l, r] $ interval equal to $ b_r-b_ {l-1} $

2, if the difference sequence have, for $ [l, r] $ $ D $ interval adding means $ b_l + d, b_ {r + 1} -d $

Look at a question:

The number of columns for the number of columns $ a $, $ Q $ times the form $ [l, r] $ all $ + d $ the number of operations, the final output changes

If you are a giant, it will Fenwick tree or tree line, Orz

We follow the second operation, make changes to the time difference sequence O (1) because the difference with the prefix and are inverse operations, prefixed final sequence difference to OK.

Not only can only do addition, as long as the inverse operation of the operation and can be the difference with the prefix, such as prefixes product (multiplied by the inverse operation is in addition to) and XOR (exclusive or inverse operation is an exclusive or) differential.

Two-dimensional differential prefix

Draw a map pushing like it

Before 缀矩 Field Service $ b_ {i, j} = b_ {i-1, j} + b_ {i, j-1} -b_ {i-1, j-1} + a_ {i, j} $.

For $ (x_1, y_1) (x_2, y_2) $ The $ + d $ submatrix i.e. difference matrix $ b_ {x_2, y_2} + d, b_ {x_1-1, y_2} -d, b_ {x_2, y_1- 1} -d, b_ {x_1-1, y_1-1} + d $

For $ (x_1, y_1) (x_2, y_2) $ prefix This matrix submatrices i.e. $ b_ {x_2, y_2} -b_ {x_1-1, y_2} -b_ {x_2, y_1-1} + b_ {x_1-1 , y_1-1} $

The remaining: Fenwick tree, a tree difference

 

Guess you like

Origin www.cnblogs.com/soledadstar/p/11600711.html