【leetcode】307. Range Sum Query - Mutable

题目如下:

解题思路:就三个字-线段树。这个题目是线段树用法最经典的场景。

代码如下:

class NumArray(object):

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        self.nl = nums
        self.tree = []
        if len(nums) == 0:
            return
        for i in xrange((4*len(nums)+1)):
            self.tree.append([])
        self.build(1,len(nums),1,nums)
        #print self.tree


    def build(self,l,r,k,nums):
        self.tree[k] = [l,r]
        if l == r: #leaf
            self.tree[k].append(nums[l-1])
            return nums[l-1]
        wl = self.build(l,(l+r)/2,2*k,nums)
        wr = self.build((l+r)/2+1,r,2*k+1,nums)
        #print l,r,wl,wr
        self.tree[k].append(wl+wr)
        return self.tree[k][2]

    def update(self, i, val):
        """
        :type i: int
        :type val: int
        :rtype: void
        """
        if i > len(self.nl):
            return
        diff = self.nl[i] - val
        #for j in range(i,len(self.sl)):
        #    self.sl[j] -= diff
        self.nl[i] = val
        k = 1
        i += 1
        while True:
            self.tree[k][2] -= diff
            m = (self.tree[k][0] + self.tree[k][1])/2
            if self.tree[k][0] == self.tree[k][1]:
                break
            if i <= m:
                k = 2*k
            else:
                k = 2*k + 1

        #print self.tree

    def calcRange(self,i,j,k):
        #print i,j,k
        if i == self.tree[k][0] and j == self.tree[k][1]:
            return self.tree[k][2]
        m = (self.tree[k][0] + self.tree[k][1])/2
        if j <= m:
            return self.calcRange(i,j,2*k)
        elif i > m:
            return self.calcRange(i,j,2*k+1)
        else:
            return self.calcRange(i,m,2*k) + self.calcRange(m+1,j,2*k+1)

    def sumRange(self, i, j):
        """
        :type i: int
        :type j: int
        :rtype: int
        """
        #print self.sl
        #print self.nl
        return self.calcRange(i+1,j+1,1)
        


# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# obj.update(i,val)
# param_2 = obj.sumRange(i,j)

 

转载于:https://www.cnblogs.com/seyjs/p/9212973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值