Python 线段树



from math import pow

class SegmentTree( object ):
    def __init__( self, left, right ):
        self.left = int( left )
        self.right = int( right )
        self.count = 0
        self.left_son, self.right_son = None, None
        if left < right:
            mid = ( left + right ) >> 1
            self.left_son = SegmentTree( left, mid )
            self.right_son = SegmentTree( mid + 1, right )

    def show_tree_struct( self ):
        import Queue
        Q = Queue.Queue( -1 )
        print( 'Tree Struct ==> [left, right, count]' )
        line = 1
        Q.put( [self,line] )
        while Q.empty() is False:
            item = Q.get()
            newline = False
            if item[1] != line:
                newline = True
                line = item[1]
            if newline:
                print
                print item[0],
            else:
                print item[0],
            son_line = item[1] + 1
            if item[0].left_son:
                Q.put( [item[0].left_son, son_line] )
            if item[0].right_son:
                Q.put( [item[0].right_son, son_line] )
        
    def insert( self, x, y ):
        if x == self.left and y == self.right:
            self.count += 1
            return
        if self.left == self.right: return
        mid = ( self.left + self.right ) / 2
        if mid < x:
            self.right_son.insert( x, y )
        elif mid >= y:
            self.left_son.insert( x, y )
        else:
            self.left_son.insert( x, mid )
            self.right_son.insert( mid + 1, y )

    def query( self, x ):
        total_count = 0
        total_count += self.count
        if self.left == self.right:
            return total_count
        mid = ( self.left + self.right ) / 2
        if x <= mid:
            total_count += self.left_son.query( x )
        if x > mid:
            total_count += self.right_son.query( x )
        return total_count

    def __str__( self ):
        return '[%s,%s,%s]'%( self.left, self.right, self.count )


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对算法有兴趣的可以来看看 在自然数,且所有的数不大于30000的范围内讨论一个问题:现在已知n条线段,把端点依次输入告诉你,然后有m个询问,每个询问输入一个点,要求这个点在多少条线段上出现过; 最基本的解法当然就是读一个点,就把所有线段比一下,看看在不在线段中; 每次询问都要把n条线段查一次,那么m次询问,就要运算m*n次,复杂度就是O(m*n) 这道题m和n都是30000,那么计算量达到了10^9;而计算机1秒的计算量大约是10^8的数量级,所以这种方法无论怎么优化都是超时 因为n条线段是固定的,所以某种程度上说每次都把n条线段查一遍有大量的重复和浪费; 线段树就是可以解决这类问题的数据结构 举例说明:已知线段[2,5] [4,6] [0,7];求点2,4,7分别出现了多少次 在[0,7]区间上建立一棵满二叉:(为了和已知线段区别,用【】表示线段树中的线段) 【0,7】 / \ 【0,3】 【4,7】 / \ / \ 【0,1】 【2,3】 【4,5】 【6,7】 / \ / \ / \ / \ 【0,0】 【1,1】 【2,2】 【3,3】 【4,4】 【5,5】 【6,6】 【7,7】 每个节点用结构体: struct line { int left,right; // 左端点、右端点 int n; // 记录这条线段出现了多少次,默认为0 }a[16]; 和堆类似,满二叉的性质决定a[i]的左儿子是a[2*i]、右儿子是a[2*i+1]; 然后对于已知的线段依次进行插入操作: 从根开始调用递归函数insert // 要插入的线段的左端点和右端点、以及当前线段树中的某条线段 void insert(int s,int t,int step)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值