-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path0307.py
34 lines (28 loc) · 909 Bytes
/
0307.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
class NumArray:
def __init__(self, nums: 'List[int]'):
self.nums = nums
self.tree = [0]*(len(nums) + 1)
for i in range(len(nums)):
self._update(i+1, nums[i])
def _update(self, i, delta):
while i < len(self.tree):
self.tree[i] += delta
i += self.lowbit(i)
def query(self, i):
res = 0
while i:
res += self.tree[i]
i -= self.lowbit(i)
return res
def lowbit(self, x):
return x & (-x)
def update(self, i: 'int', val: 'int') -> 'None':
self._update(i+1, val - self.nums[i])
self.nums[i] = val
def sumRange(self, i: 'int', j: 'int') -> 'int':
return self.query(j+1) - self.query(i)
if __name__ == "__main__":
nums = [0,9,5,7,3]
a = NumArray(nums)
print(a.sumRange(4, 4))
print(a.sumRange(2, 4))