2D BIT supports:
- Point update at
- Prefix sum query for rectangle to
The structure nests the 1D operations:
def update2D(x, y, delta):
i = x
while i <= n:
j = y
while j <= m:
tree[i][j] += delta
j += j & (-j)
i += i & (-i)
def query2D(x, y):
result = 0
i = x
while i > 0:
j = y
while j > 0:
result += tree[i][j]
j -= j & (-j)
i -= i & (-i)
return result
Time: for both operations. Space: .