-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryIndexTree.py
More file actions
45 lines (37 loc) · 935 Bytes
/
BinaryIndexTree.py
File metadata and controls
45 lines (37 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 18 13:51:27 2019
@author: cosmic
"""
class BITree:
def __init__(self,nums):
n = len(nums)
self.bit = [0]*(n+1)
print(self.bit)
self.n = n
for i in range(n):
self.__update(self.bit,n,i,nums[i])
def __update(self,bit,n,i,v):
i += 1
while i<=n:
bit[i] += v
i += i & (-i)
def update(self,i,v):
#add v to nums[i]
self.__update(self.bit,self.n,i,v)
def __query(self,bit,i):
s = 0
i += 1
while i>0:
s += bit[i]
i -= i&(-i)
return s
def query(self,i):
assert i<self.n
return self.__query(self.bit,i)
nums = [4,5,1,3,7,2,6]
ob = BITree(nums)
print(ob.bit)
#ob.update(0,5)
#print(ob.bit)
print(ob.query(3))