Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3d Fenwick tree

I have a three-dimensional fenwick tree data structure. I need to calculate sum on some segment from (x0, y0, z0) to (x, y, z)

What is the formula for inclusion-exclusion? For instance, for 2D variant it is

s = sum(x, y) - sum(x, y0 - 1) - sum(x0 - 1, y) + sum(x0 - 1, y0 - 1)

Thanks in advance

http://www.comp.nus.edu.sg/~stevenha/ft.pdf

Here is 2D case: enter image description here

like image 378
Denys Astanin Avatar asked Jul 29 '12 22:07

Denys Astanin


People also ask

Is Fenwick tree better than segment tree?

Fenwick trees are faster and extremely simple to implement. The asymptotic bounds are equivalent, but the most basic query and update code is almost branchless, non-recursive, and uses very few operations. The segment tree versions of this can be made almost as fast, but this does take extra effort.

Is Fenwick tree useful?

A Fenwick tree or binary indexed tree is a data structure that helps compute prefix sums efficiently. Computing prefix sums are often important in various other algorithms, not to mention several competitive programming problems. For example, they are used to implement the arithmetic coding algorithm.

What does Fenwick tree do?

A Fenwick tree or binary indexed tree is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers. This structure was proposed by Boris Ryabko in 1989 with a further modification published in 1992.

Is binary indexed tree and Fenwick tree same?

A Fenwick tree, also called a binary indexed tree (BIT), is a data structure that can efficiently update elements and calculate range sums on a list of numbers.


1 Answers

formula involves total 8 computations

value1 = sum(x,y,z)- sum(x0-1,y,z)  - sum(x,y0-1,z) + sum(x0-1,y0-1,z)

value2 = sum(x,y,z0-1) - sum(x0-1,y,z0-1) - sum(x,y0-1,z0-1)  + sum(x0-1,y0-1,z0-1)


final answer = value1 - value2

Time complexity of code is 8 (logn)^3

How can you visualize.

1> assume it as a cube.

2> value 1 gives answer for upper square layer of cube, but it include sum upto 0 in z-axis. 

You only want upto z0, so you have subtract that extra part(from z0 to 0, and that is value2).
like image 188
TLE Avatar answered Sep 30 '22 01:09

TLE