-
Notifications
You must be signed in to change notification settings - Fork 139
/
fenwick_tree.py
41 lines (36 loc) · 844 Bytes
/
fenwick_tree.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
n = 5
tree = [0] * (n + 1)
''' Fenwick Tree (Binary Indexed Tree) '''
def update(i, dif):
global n
while i <= n:
tree[i] += dif
i += (i & -i)
def update_to(i, to):
update(i, to - interval_sum(i, i))
def get_sum(i):
res = 0
while i > 0:
res += tree[i]
i -= (i & -i)
return res
def interval_sum(start, end):
return get_sum(end) - get_sum(start - 1)
# Set all the data input
update_to(1, 1)
update_to(2, 2)
update_to(3, 3)
update_to(4, 4)
update_to(5, 5)
# Interval sum from index 2 to index 5.
print(interval_sum(2, 5))
# Update index 3.
update(3, -2)
# Interval sum from index 2 to index 5.
print(interval_sum(2, 5))
# Update index 5.
update(5, 2)
# Interval sum from index 2 to index 5.
print(interval_sum(2, 5))
# Interval sum from index 1 to index 5.
print(interval_sum(1, 5))