manish icon

Binary Indexed Tree (BIT)

manish | PRO | 10/26/19 08:10:07 AM UTC | 0 ⭐ | 446 👁️ | Never ⏰ | []
Python |

440 B

|

None

|

0 👍

/

0 👎

class Bit:
    def __init__(self, a):
        self.a = a
        self.n = len(a)
        self.bit = [0] * (self.n + 1)
        for i, x in enumerate(a):
            self.update(i+1, x)
 
    def update(self, x, val):
        while x <= self.n:
            self.bit[x] += val
            x += x & -x
 
    def query(self, x):
        sum = 0
        while x > 0:
            sum += self.bit[x]
            x -= x & -x
        return sum

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎