-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path307_RangeSumQueryMutable.py
executable file
·55 lines (47 loc) · 1.43 KB
/
307_RangeSumQueryMutable.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
# Obvious PUIQ(Point Update Interval Query) question.
# Can be solved with a fenwick tree(Binary Indexed Trees)
# Or use segment tree to solve it.
class NumArray(object):
"""
1. Binary Indexed Trees.
Here is the clear explanation about Binary Indexed Tree:
http://blog.jobbole.com/96430/#
"""
def __init__(self, nums):
self.n = len(nums)
self.nums = nums
self.sum_tree = [0] * (self.n+1)
for i in range(self.n):
self._add(i+1, nums[i])
def _add(self, i, val):
while i <= self.n:
self.sum_tree[i] += val
i += (i & -i)
# Get the sum of array nums[0:i], inclusive.
def _sum(self, i):
sum_val = 0
while i > 0:
sum_val += self.sum_tree[i]
i -= (i & -i)
return sum_val
# Pay attention to the meanning of num & -num.
# def _lowbit(self, num):
# return num & -num
def update(self, i, val):
self._add(i+1, val - self.nums[i])
self.nums[i] = val
def sumRange(self, i, j):
if not self.nums:
return 0
# sum of elements nums[i..j], inclusive.
return self._sum(j+1) - self._sum(i)
"""
if __name__ == '__main__':
numArray = NumArray([1, 3, 5, 7, 8, 10])
print numArray.sumRange(0, 4)
numArray.update(1, 1)
print numArray.sumRange(1, 3)
"""