Advertisement
manish

Binary Indexed Tree (BIT)

Oct 26th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. class Bit:
  2.     def __init__(self, a):
  3.         self.a = a
  4.         self.n = len(a)
  5.         self.bit = [0] * (self.n + 1)
  6.         for i, x in enumerate(a):
  7.             self.update(i+1, x)
  8.  
  9.     def update(self, x, val):
  10.         while x <= self.n:
  11.             self.bit[x] += val
  12.             x += x & -x
  13.  
  14.     def query(self, x):
  15.         sum = 0
  16.         while x > 0:
  17.             sum += self.bit[x]
  18.             x -= x & -x
  19.         return sum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement