python线段树

class Node:
    def __init__(this,l,r,vMax,vSum):
        this.l=l
        this.r=r
        this.vMax=vMax
        this.vSum=vSum
        this.fSum=0
        this.fMax=0
    def __init__(this):
        pass
class SegTree:
    def __init__(this,n,arr):
        this.tree=[Node() for i in range(4*n+1)]
        this.arr=list(arr)
    
    def build(this,l,r,k):
        this.tree[k].l=l
        this.tree[k].r=r
        if l==r:
            this.tree[k].vMax=this.arr[l-1]
            this.tree[k].vSum=this.arr[l-1]
            return

        m=(l+r)//2
        this.build(l,m,k*2)
        this.build(m+1,r,k*2+1)
        this.tree[k].vSum=this.tree[k*2].vSum+this.tree[k*2+1].vSum
        this.tree[k].vMax=max(this.tree[k*2].vMax,this.tree[k*2+1].vMax)

    def askMax(this,k,x,y):
        if (this.tree[k].l>=x and this.tree[k].r<=y):
            return this.tree[k].vMax
        
        res=0
        m=(this.tree[k].l+this.tree[k].r)//2
        if x<=m: 
            res=max(res,this.askMax(k*2,x,y))
        if y>m:
            res=max(res,this.askMax(k*2+1,x,y))
        return res
    
    def askSum(this,k,x,y):
        if (this.tree[k].l>=x and this.tree[k].r<=y):
            return this.tree[k].vSum
        
        res=0
        m=(this.tree[k].l+this.tree[k].r)//2
        if x<=m:
            res+=this.askSum(k*2,x,y)
        if y>m:
            res+=this.askSum(k*2+1,x,y)
        return res

    def modify(this,k,id,val,gap):
        this.tree[k].vSum+=gap
        #this.tree[k].vMax=max(this.tree[k].vMax,val)
        if this.tree[k].l==this.tree[k].r:
            this.arr[id-1]=val
            this.tree[k].vMax=val
            return
        m=(this.tree[k*2].l+this.tree[k*2+1].r)//2
        if id<=m:
            this.modify(k*2,id,val,gap)
        if id>m:
            this.modify(k*2+1,id,val,gap)
        this.tree[k].vMax=max(this.tree[k*2].vMax,this.tree[k*2+1].vMax)


n,m=list(map(int,input().split()))
arr=list(map(int,input().split()))
segTree=SegTree(n,arr)
segTree.build(1,n,1)

for i in range(m):
    p,x,y=list(map(int,input().split()))

    if p==1:
        segTree.modify(1,x,y,y-segTree.arr[x-1])
    elif p==2:
        print(segTree.askSum(1,x,y))
    else:
        print(segTree.askMax(1,x,y))```

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值