Fenwick tree and a simple extension

Fenwick tree and a simple extension

Fenwick tree did not explain in detail, only to do a summary of the application type.

A one-dimensional array of tree

A single point of modification, the query interval

Because Fenwick tree maintenance is a prefix information, the direct use of the prefix query interval properties can be subtracted.
Carte modified only from the starting position to update backward.

Modify the interval, a single point of inquiry

Fenwick tree supports only a single point of modification, how to deal with the interval amend it?
Consider a common range of modifications to single-point modification routines: differential
and the front differential array \ (n \) key and also happens is the real \ (v_i \) .
All we can do a direct difference.

Modify the interval, the interval query

Similarly, the first difference, the difference after the interval modification is no different.
And here we find that we can only get a single point corresponding to the value, but not directly correspond to the intervals and.
Are we going to set a way of summing interval data ? structure Well the answer is no
, we found that we require is actually this:
\ [\ sum_ {i = L} ^ r {\ sum_ {J = 1} ^ i} {D_J} \]
where, \ (D_J \) . for the inner sum is the difference array apparently can query directly in the tree to get the array
so how do deal with the outer layer again? \ (for \) again is obviously impossible.
so we have to rethink:
order \ ( sum_i \) represents the prefix and a.
\ [\ the begin {Array} {C} {\ OperatorName {SUM} [I] = \ sum_ {J =. 1} ^ {I} V [J] + \ sum_ {J =. 1} ^ {i} \ operatorname {d } [j] * (i-j + 1)} \\ {\ operatorname {sum} [i] = \ sum_ {j = 1} ^ {i} v [j] + ( i + 1) * \ sum_ {
j = 1} ^ {i} d [j] - \ sum_ {j = 1} ^ {i} d [j] * j} \ end {array} \] so just go maintain a prefix and the original array, and a tree before the same array, and other maintenance \ (d_j \ times j \) of Fenwick tree can be.

Two-dimensional array of tree

A single point of modification, matrix inquiry

Directly to modify a single point, two-dimensional forms and prefix query.

Matrix modification, a single point of inquiry

After the two-dimensional differential, each matrix can be modified only need to modify at four locations.

Single point queries directly to seek a two-dimensional and two-dimensional prefix Fenwick tree.

Modified matrix, the matrix inquiry

The same difference or the first two-dimensional matrix can be modified directly modify a single point in four locations.

Then the matrix inquiry? We encountered the same problem as above.

This time we asked this equation becomes:

\[\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{i=k}^{n}\sum_{j=l}^{m}{d_{k,l}}\]

Enumeration change my order get:

\[\sum_{k=1}^{n}\sum_{l=1}^{m}\sum_{i=k}^{n}\sum_{j=l}^{m}d_{k,l}\]

In this case, \ (D_ {K, L} \) and \ (i, j \) independent, it can be rewritten as:

\[\sum_{k=1}^{n}\sum_{l=1}^{m}{d_{k,l}\times (n-k+1)\times (m-l+1)}\]

Out of habit, put it writing:

\[\sum_{i=1}^{n}\sum_{j=1}^{m}{d_{i,j}\times (n-i+1)\times (m-j+1)}\]

Consider demolition term yields:

\[\left(\sum_{i=1}^{n} \sum_{j=1}^{m} d_{i, j}\right) \times(n+1)\times (m+1)-\sum_{i=1}^{n} \sum_{j=1}^{m} i \times d_{i, j} \times(m+1)-\sum_{i=1}^{n} \sum_{j=1}^{m} j \times d_{i, j} \times(n+1)+\sum_{i=1}^{n} \sum_{j=1}^{m} i \times j \times d_{i, j}\]

So, like the one above and one-dimensional case, the maintenance of four different Fenwick tree can be.

Summary

Although there is a saying: all operations, segment tree Fenwick tree that can be achieved can be achieved
. But very often still Fenwick tree is more simple, efficient
Fenwick tree has a tree line can not match the high efficiency and low difficulty of the code, the only drawback is that some operations Fenwick tree array tree can not or difficult to maintain.
For example at intervals of one-dimensional and two-dimensional case most value problem, If you insist on using the array to implement a tree, so will be more of a \ (log \) the complexity of the situation will be more two-dimensional \ (log \) complexity to.

Therefore, the case can be used Fenwick tree, not to use the tree line is a wise choice, after all, time and space complexity of Fenwick tree is better than the tree line. (Except outside the range of most value this kind of problem

Guess you like

Origin www.cnblogs.com/Equinox-Flower/p/11668498.html