-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
fenwick.py
70 lines (58 loc) · 2.36 KB
/
fenwick.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
@author: shakeelsamsu
https://gihtub.com/shakeelsamsu
Oct 7 18 9:42:35 PM
"""
class fenwick:
"""Fenwick Tree"""
# def __init__(self, list):
# """Initialize BIT with list in O(n*log(n))"""
# self.array = [0] * (len(list) + 1)
# for idx, val in enumerate(list):
# self.update(idx, val)
def __init__(self, list):
""""Initialize BIT with list in O(n)"""
self.array = [0] + list
for idx in range(1, len(self.array)):
idx2 = idx + (idx & -idx)
if idx2 < len(self.array):
self.array[idx2] += self.array[idx]
def prefix_query(self, idx):
"""Computes prefix sum of up to including the idx-th element"""
idx += 1
result = 0
while idx:
result += self.array[idx]
idx -= idx & -idx
return result
def range_query(self, from_idx, to_idx):
"""Computes the range sum between two indices (both inclusive)"""
return self.prefix_query(to_idx) - self.prefix_query(from_idx - 1)
def update(self, idx, add):
"""Add a value to the idx-th element"""
idx += 1
while idx < len(self.array):
self.array[idx] += add
idx += idx & -idx
if __name__ == '__main__':
array = [1, 7, 3, 0, 5, 8, 3, 2, 6, 2, 1, 1, 4, 5]
bit = fenwick(array)
print('Array: [{}]'.format(', '.join(map(str, array))))
print()
print('Prefix sum of first {} elements: {}'.format(13,
bit.prefix_query(12)))
print('Prefix sum of first {} elements: {}'.format(7, bit.prefix_query(6)))
print('Range sum from pos {} to pos {}: {}'.format(1, 5,
bit.range_query(1, 5)))
print()
bit.update(4, 2)
print('Add {} to element at pos {}'.format(2, 4))
new_array = [bit.range_query(idx, idx) for idx in range(len(array))]
print('Array: [{}]'.format(', '.join(map(str, new_array))))
print()
print('Prefix sum of first {} elements: {}'.format(13,
bit.prefix_query(12)))
print('Prefix sum of first {} elements: {}'.format(7, bit.prefix_query(6)))
print('Range sum from pos {} to pos {}: {}'.format(1, 5,
bit.range_query(1, 5)))
print()