-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path307_range_sum_query_mutable.py
More file actions
60 lines (48 loc) · 1.35 KB
/
307_range_sum_query_mutable.py
File metadata and controls
60 lines (48 loc) · 1.35 KB
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
"""
REF: https://cs.stackexchange.com/questions/10538/bit-what-is-the-intuition-behind-a-binary-indexed-tree-and-how-was-it-thought-a
REF: http://www.cnblogs.com/grandyang/p/4985506.html
Your NumArray object will be instantiated and called as such:
obj = NumArray(nums)
obj.update(i,val)
param_2 = obj.sumRange(i,j)
"""
class NumArray:
def __init__(self, nums):
"""
:type nums: List[int]
"""
if not nums:
return
n = len(nums)
self.bits = [0] * (n + 1) # bits
self.incr = [0] * (n + 1) # increments
for i in range(n):
self.update(i, nums[i])
def update(self, i, val):
"""
:type i: int
:type val: int
:rtype: void
It must increase `i` here, since this api is public,
so look from outside, the `i` is just the index of `nums`
"""
j = i + 1
delta = val - self.incr[j]
self.incr[j] = val
while j < len(self.incr):
self.bits[j] += delta
j += (j & -j)
def sumRange(self, i, j):
"""
:type i: int
:type j: int
:rtype: int
"""
return self.sum(j + 1) - self.sum(i)
def sum(self, i):
res = 0
j = i
while j > 0:
res += self.bits[j]
j -= (j & -j)
return res