这题Python过不了?

P3372 【模板】线段树 1

WaTleZero_pt @ 2023-11-09 18:52:06

实在无聊就想着用Python 3把这道题重写一遍,可是最后三个TLE了?


s=[0]*400010
lay=[0]*400010
val=[0]*400010
def lc(x):
    return x*2
def rc(x):
    return x*2+1
def pushup(u):
    s[u]=s[lc(u)]+s[rc(u)];
def build(l,r,u):
    if(l==r):
        lay[u]=0
        s[u]=val[l]
        return
    else:
        m=(l+r)//2
        build(l,m,lc(u))
        build(m+1,r,rc(u))
        pushup(u)
def pushdown(l,r,u):
    m=(l+r)//2
    s[lc(u)]+=(m-l+1)*lay[u]
    lay[lc(u)]+=lay[u]
    s[rc(u)]+=(r-m)*lay[u]
    lay[rc(u)]+=lay[u]
    lay[u]=0
def update(l,r,tl,tr,x,u):
    if(tl<=l and r<=tr):
        s[u]+=(r-l+1)*x
        lay[u]+=x
    else:
        pushdown(l,r,u)
        m=(l+r)//2
        if(tl<=m):
            update(l,m,tl,tr,x,lc(u))
        if(m<tr):
            update(m+1,r,tl,tr,x,rc(u))
        pushup(u)
def query(l,r,tl,tr,u):
    if(tl<=l and r<=tr):
        return s[u]
    else:
        pushdown(l,r,u)
        m=(l+r)//2
        ans=0
        if(tl<=m):
            ans+=query(l,m,tl,tr,lc(u))
        if(m<tr):
            ans+=query(m+1,r,tl,tr,rc(u))
        return ans

def main():
    lis=input().split()
    n=int(lis[0])
    m=int(lis[1])
    lis=input().split()
    for i in range(1,n+1,1):
        val[i]=int(lis[i-1])
    build(1,n,1)
    for i in range(0,m,1):
        lis=input().split()
        if(int(lis[0])==1):
            update(1,n,int(lis[1]),int(lis[2]),int(lis[3]),1)
        elif(int(lis[0])==2):
            print(query(1,n,int(lis[1]),int(lis[2]),1))
main()

``

by sfqxx1 @ 2023-11-09 18:56:16

@WaTleZero_pt 用 pypy3。


by WaTleZero_pt @ 2023-11-09 18:57:56

好的现在过了。此贴结。 @sfqxx1


by WaTleZero_pt @ 2023-11-09 19:00:27

@sfqxx1 不是小伙纸你交了我的代码?


by sfqxx1 @ 2023-11-09 19:08:12

@WaTleZero_pt 我帮你测试啊,没有过我就不管了(


by sfqxx1 @ 2023-11-09 19:08:30

@WaTleZero_pt 我早就过了


|