1. 程式人生 > 實用技巧 >307. 區域和檢索 - 陣列可修改

307. 區域和檢索 - 陣列可修改

給定一個整數陣列 nums,求出陣列從索引i到j(i≤j) 範圍內元素的總和,包含i, j兩點。

update(i, val) 函式可以通過將下標為i的數值更新為val,從而對數列進行修改。

示例:

Given nums = [1, 3, 5]

sumRange(0, 2) -> 9
update(1, 2)
sumRange(0, 2) -> 8
說明:

陣列僅可以在update函式下進行修改。
你可以假設 update 函式與 sumRange 函式的呼叫次數是均勻分佈的。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/range-sum-query-mutable


線段樹板子

class NumArray:

    def __init__(self, nums: List[int]):
        self.n=len(nums)
        if self.n==0:return
        max_size= 2 ** (int(math.ceil(math.log(self.n, 2))) + 1) - 1
        self.seg_tree=[0 for i in range(max_size)]
        self.arr=nums
        self.biuld_tree(0,0,self.n-1)
    
    
def biuld_tree(self,node,start,end): if start==end: self.seg_tree[node]=self.arr[start] return mid=(start+end)//2 left_node=2*node+1 right_node=left_node+1 self.biuld_tree(left_node,start,mid) self.biuld_tree(right_node,mid+1,end) self.seg_tree[node]
=self.seg_tree[left_node]+self.seg_tree[right_node] def update(self, i: int, val: int) -> None: def update_tree(node,start,end,idx,val): if start==end: self.seg_tree[node]=self.arr[idx]=val return mid=(start+end)//2 left_node=2*node+1 right_node=left_node+1 if start<=idx<=mid: update_tree(left_node,start,mid,idx,val) else: update_tree(right_node,mid+1,end,idx,val) self.seg_tree[node]=self.seg_tree[left_node]+self.seg_tree[right_node] update_tree(0,0,self.n-1,i,val) def sumRange(self, i: int, j: int) -> int: def query_tree(node,start,end,l,r): if l>end or r<start:return 0 elif l<=start and end<=r:return self.seg_tree[node] elif start==end:return self.seg_tree[node] mid=(start+end)//2 left_node=2*node+1 right_node=left_node+1 sum_left=query_tree(left_node,start,mid,l,r) sum_right=query_tree(right_node,mid+1,end,l,r) return sum_left+sum_right return query_tree(0,0,self.n-1,i,j) # Your NumArray object will be instantiated and called as such: # obj = NumArray(nums) # obj.update(i,val) # param_2 = obj.sumRange(i,j)