forked from rafaelalmeida2909/Python-Simple-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfenwick_tree.py
More file actions
27 lines (22 loc) · 751 Bytes
/
Copy pathfenwick_tree.py
File metadata and controls
27 lines (22 loc) · 751 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
class FenwickTree:
def __init__(self, size):
self.size = size
self.fwick_tree = [0] * (size + 1)
def prefix_sum(self, position):
sum = 0
while position > 0:
sum += self.fwick_tree[position]
position -= position & -position
return sum
def range_sum(self, left_pos, right_pos):
return self.prefix_sum(right_pos) - self.prefix_sum(left_pos - 1)
def point_update(self, position, value):
index = position
while index <= self.size:
self.fwick_tree[index] += value
index += index & -index
if __name__ == '__main__':
FT = FenwickTree(5)
FT.point_update(3, 1)
FT.point_update(4, 3)
print(FT.range_sum(3, 4))