3_Problems_Everyday's blog

By 3_Problems_Everyday, history, 7 years ago, In English

In 1-D we can find sum in range [l, r] using sum[r] - sum[l - 1].

In 2-D we can find sum in range from (x1, y1) to (x2, y2) using sum[x2][y2] - sum[x2][y1] - sum[x1][y2] + sum[x1][y1] where sum[] or sum[][] is cumulative sum .

How to find sum in range from (x1, y1, z1) to (x2, y2, z2) in a similar way ?

»
7 years ago, # |
  Vote: I like it +11 Vote: I do not like it

It keeps extending by inclusion exclusion. 1D is (0) — (-1) 2D is (0,0) — (0,-1) — (-1,0) + (-1,-1) 3D is (0,0,0) — (-1,0,0) — (0,-1,0) — (0,0,-1) + (-1,-1,0) + (-1,0,-1) + (0,-1,-1) — (-1,-1,-1) and so on