-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path307.py
More file actions
33 lines (30 loc) · 910 Bytes
/
Copy path307.py
File metadata and controls
33 lines (30 loc) · 910 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
28
29
30
31
32
33
class NumArray:
def __init__(self, nums: List[int]):
self.n = len(nums)
self.vector = [0 for _ in range(2 * self.n)]
j = 0
for i in range(self.n, 2 * self.n):
self.vector[i] = nums[j]
j += 1
for i in range(self.n - 1, 0, -1):
self.vector[i] = self.vector[i * 2] + self.vector[i * 2 + 1]
def update(self, i: int, val: int) -> None:
i += self.n
prev = self.vector[i]
while i > 0:
self.vector[i] += val - prev
i //= 2
def sumRange(self, i: int, j: int) -> int:
sum = 0
i += self.n
j += self.n
while i <= j:
if i % 2 == 1:
sum += self.vector[i]
i += 1
if j % 2 == 0:
sum += self.vector[j]
j -= 1
i //= 2
j //= 2
return sum