結果

問題 No.1234 典型RMQ
ユーザー 👑 KazunKazun
提出日時 2020-09-20 20:15:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 5,230 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 105,288 KB
最終ジャッジ日時 2023-09-27 06:54:57
合計ジャッジ時間 18,895 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 67 ms
71,540 KB
testcase_02 AC 67 ms
71,436 KB
testcase_03 WA -
testcase_04 AC 68 ms
71,488 KB
testcase_05 AC 68 ms
71,364 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 440 ms
102,748 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 67 ms
71,516 KB
testcase_28 AC 68 ms
71,396 KB
testcase_29 AC 68 ms
71,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Lazy_Evaluation_Tree():
    def __init__(self,L,calc,unit,op,comp,id):
        """calcを演算,opを作用とするリストLのSegment Treeを作成

        calc:演算
        unit:モノイドcalcの単位元 (xe=ex=xを満たすe)
        op:作用素
        comp:作用素の合成
        id:恒等写像

        [条件] M:Monoid,F={f:F x M→ M:作用素}に対して,以下が成立する.
        Fは恒等写像 id を含む.つまり,任意の x in M に対して id(x)=x
        Fは写像の合成に閉じている.つまり,任意の f,g in F に対して, comp(f,g) in F
        任意の f in F, x,y in M に対して,f(xy)=f(x)f(y)である.

        [注記]
        作用素は左から掛ける.更新も左から.
        """

        self.calc=calc
        self.unit=unit
        self.op=op
        self.comp=comp
        self.id=id

        N=len(L)

        d=max(1,(N-1).bit_length())
        k=1<<d

        X=[unit]*(k-1)+L+[unit]*(k-len(L))

        self.num=k
        self.depth=d

        for i in range(k-2,-1,-1):
            X[i]=calc(X[(i<<1)+1],X[(i<<1)+2])

        self.data=X
        self.lazy=[self.id]*((k<<1)-1)

    #配列の第k要素の真上を伝搬
    def propagation(self,k,index=0):
        x=k
        V=[k]
        while x:
            x=(x-1)>>1
            V.append(x)

        for x in  V[::-1]:
            if self.lazy[x]==self.id:
                continue

            self.data[x]=self.op(self.lazy[x],self.data[x])

            if x<self.num-1:
                self.lazy[(x<<1)+1]=self.comp(self.lazy[x],self.lazy[(x<<1)+1])
                self.lazy[(x<<1)+2]=self.comp(self.lazy[x],self.lazy[(x<<1)+2])

            self.lazy[x]=self.id



    def recalculate(self,k,index=0):
        x=k-index
        while True:
            x=(x-1)>>1

            if x<0:
                break

            lc=(x<<1)+1
            rc=(x<<1)+2

            if self.lazy[lc]==self.id:
                a=self.data[lc]
            else:
                a=self.op(self.lazy[lc],self.data[lc])

            if self.lazy[rc]==self.id:
                b=self.data[rc]
            else:
                b=self.op(self.lazy[rc],self.data[rc])
            self.data[x]=self.calc(a,b)


    def get(self,k,index=0):
        """ 第k要素を取得

        """
        m=(k-index)+(self.num-1)
        self.propagation(m,0)
        self.recalculate(m,0)

        return self.data[m]

    def operate(self,From,To,alpha,index=0,left_closed=True,right_closed=True):
        L=From-index+(not left_closed)+self.num-1
        R=To-index-(not right_closed)+self.num-1

        L0=R0=None
        X=L
        Y=R

        while X<Y:
            if X&1==0:
                if L0==None:
                    L0=X
                X+=1

            if Y&1:
                if R0==None:
                    R0=Y
                Y-=1
            X=(X-1)>>1
            Y=(Y-1)>>1

        if L0==None:
            L0=X
        if R0==None:
            R0=Y

        self.propagation(L0)
        self.propagation(R0)

        while L<R:
            if L&1==0:
                self.lazy[L]=self.comp(alpha,self.lazy[L])
                L+=1

            if R&1:
                self.lazy[R]=self.comp(alpha,self.lazy[R])
                R-=1

            L=(L-1)>>1
            R=(R-1)>>1

        if L==R:
            self.lazy[L]=self.comp(alpha,self.lazy[L])

        self.recalculate(L0)
        self.recalculate(R0)

    def update(self,k,beta,index=0):
        """ 第k要素をbetaに変更する.

        """
        m=(k-index)+(self.num-1)

        self.propagation(m,0)

        self.lazy[m]=self.id
        self.data[m]=beta

        self.recalculate(m,0)

    def product(self,From,To,index=0,left_closed=True,right_closed=True):
        L=From-index+(not left_closed)+self.num-1
        R=To-index-(not right_closed)+self.num-1

        L0=R0=None
        X=L
        Y=R

        while X<Y:
            if X&1==0:
                if L0==None:
                    L0=X
                X+=1

            if Y&1:
                if R0==None:
                    R0=Y
                Y-=1
            X=(X-1)>>1
            Y=(Y-1)>>1

        if L0==None:
            L0=X
        if R0==None:
            R0=Y

        self.propagation(L0)
        self.propagation(R0)

        vL=self.unit
        vR=self.unit
        while L<R:
            if L&1==0:
                vL=self.calc(vL,self.data[L])
                L+=1

            if R&1:
                vR=self.calc(self.data[R],vR)
                R-=1

            L=(L-1)>>1
            R=(R-1)>>1

        if L==R:
            return self.calc(self.calc(vL,self.data[L]),vR)
        else:
            return self.calc(vL,vR)

    def product_all(self):
        return self.product(0,self.num-1)
#================================================
N=int(input())
A=list(map(int,input().split()))

Q=int(input())

calc=lambda x,y:min(x,y)
unit=float("inf")
op=lambda a,x:a+x
comp=lambda a,b:a+b
id=0

S=Lazy_Evaluation_Tree(A,calc,unit,op,comp,id)

X=[]
for _ in range(Q):
    k,l,r,c=map(int,input().split())
    if k==1:
        S.operate(l,r,c,1)
    else:
        X.append(S.product(l,r,1))

print("\n".join(map(str,X)))
0