結果

問題 No.1234 典型RMQ
ユーザー convexineqconvexineq
提出日時 2024-04-03 01:53:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 7,733 bytes
コンパイル時間 362 ms
コンパイル使用メモリ 82,292 KB
実行使用メモリ 96,612 KB
最終ジャッジ日時 2024-09-30 23:33:31
合計ジャッジ時間 7,876 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 39 ms
54,400 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 39 ms
53,760 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 192 ms
95,360 KB
testcase_21 WA -
testcase_22 AC 244 ms
96,240 KB
testcase_23 AC 239 ms
96,248 KB
testcase_24 AC 237 ms
96,196 KB
testcase_25 AC 239 ms
96,612 KB
testcase_26 AC 238 ms
96,324 KB
testcase_27 AC 39 ms
54,016 KB
testcase_28 AC 37 ms
54,272 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class LazySegmentTreeCommutative:
    #seg = LazySegmentTree(op_X, e_X, mapping, composision_of_Aut_X, id_of_Aut_X, N, array=None):
    def __init__(self, op_X, e_X, mapping, composision_of_Aut_X, id_of_Aut_X, N, array=None):
        #  それぞれ  Xの演算, 単位元, f(x), f\circ g,             Xの恒等変換
        # M が X に作用する
        #__slots__ = ["op_X",  "e_X",  "mapping","compose","id_M","N","log","N0","data","lazy"]
        self.e_X = e_X; self.op_X = op_X; self.mapping = mapping; self.compose = composision_of_Aut_X; self.id_M = id_of_Aut_X
        self.N = N
        self.log = (N-1).bit_length()
        self.N0 = 1<<self.log
        self.data = [e_X]*(2*self.N0)
        self.lazy = [self.id_M]*(2*self.N0)
        if array is not None:
            assert N == len(array)
            self.data[self.N0:self.N0+self.N] = array
            for i in range(self.N0-1,0,-1):self.update(i)

    ###################################### 以下内部関数
    def update(self, k):
        self.data[k] = self.op_X(self.data[2*k], self.data[2*k+1])

    def _propagate_above(self, x):
        for i in range(1,x.bit_length())[::-1]:
            k = x>>i
            #self.push(k)
            if self.lazy[k] is self.id_M: continue
            self.data[2*k  ] = self.mapping(self.lazy[k], self.data[2*k])
            self.data[2*k+1] = self.mapping(self.lazy[k], self.data[2*k+1])
            if 2*k < self.N0:
                self.lazy[2*k]   = self.compose(self.lazy[k], self.lazy[2*k])
                self.lazy[2*k+1] = self.compose(self.lazy[k], self.lazy[2*k+1])
            self.lazy[k] = self.id_M
    
    def _update_above(self,k):
        while k >= 2:
            k >>= 1
            self.data[k] = self.mapping(self.lazy[k],self.op_X(self.data[2*k], self.data[2*k+1]))

    def push(self, k): #propagate と同じ
        if self.lazy[k] is self.id_M: return
        self.data[2*k  ] = self.mapping(self.lazy[k], self.data[2*k])
        self.data[2*k+1] = self.mapping(self.lazy[k], self.data[2*k+1])
        if 2*k < self.N0:
            self.lazy[2*k]   = self.compose(self.lazy[k], self.lazy[2*k])
            self.lazy[2*k+1] = self.compose(self.lazy[k], self.lazy[2*k+1])
        self.lazy[k] = self.id_M

    def _make_binarytree_string(self,lst):
        def str_modify(x):
            return "INF" if type(x)==int and x >= 10**18+1 else str(x)
        
        A = list(map(str_modify,lst))
        N = len(lst); assert N&(N-1)==0 # pow of 2
        spacestring = [" "*len(A[N//(i&-i)//2 + i//(i&-i)//2]) for i in range(1,N)]
    
        res = []    
        for i in range(N.bit_length()-1):
            base = 1<<i
            r = spacestring[:]
            for j,v in enumerate(A[base:2*base]):
                r[N//base//2-1+j*N//base] = v
            res.append("".join(r))
        return "\n".join(res)
    ####################################### ここまで内部関数

    def __str__(self):
        res = []
        s = self._make_binarytree_string(seg.lazy).split("\n")
        t = self._make_binarytree_string(seg.data).split("\n")
        for i,j in zip(s,t):
            res.append(i + " | " + j)
        return "\n".join(res)
    
    # 1点更新
    def point_set(self, p, x):
        p += self.N0
        self.lazy[p] = self.id_M
        self.data[p] = x
        self._update_above(p)

    # 1点取得
    def point_get(self, p):
        p += self.N0
        v = self.data[p]
        while p >= 2:
            p >>= 1
            v = self.mapping(self.lazy[p],v)
        return v
 
    # 半開区間[L,R)をopでまとめる
    def prod(self, l, r):
        if l == r: return self.e_X
        l += self.N0
        r += self.N0
        LCA = l >> (l^(r-1)).bit_length()
        l,r = l//(l&-l), r//(r&-r)
        sml = self.e_X if l != LCA else self.data[LCA]
        smr = self.e_X if r-1 != LCA else self.data[LCA]
        while LCA < l:
            if l & 1: 
                sml = self.op_X(sml, self.data[l])
                l += 1
            l >>= 1
            sml = self.mapping(self.lazy[l-1], sml)
        while LCA < r-1:
            if r & 1:
                r -= 1
                smr = self.op_X(self.data[r], smr)
            r >>= 1
            smr = self.mapping(self.lazy[r], smr)
        sml = self.op_X(sml,smr)
        while LCA >= 2:
            LCA >>= 1
            sml = self.mapping(self.lazy[LCA],sml)
        return sml
 
    # 全体をopでまとめる
    def all_prod(self): return self.data[1]
 
    # 1点作用
    def apply_point(self, p, f):
        p += self.N0
        self.data[p] = self.mapping(f, self.data[p])
        self._update_above(p)

    # 区間作用
    def apply(self, l, r, f):
        if l == r: return
        l += self.N0
        r += self.N0
        L,R = l//(l&-l), r//(r&-r)
        while l < r:
            if l & 1: 
                self.data[l] = self.mapping(f, self.data[l])
                self.lazy[l] = self.compose(f, self.lazy[l])
                l += 1
            if r & 1:
                r -= 1
                self.data[r] = self.mapping(f, self.data[r])
                self.lazy[r] = self.compose(f, self.lazy[r])
            l >>= 1
            r >>= 1
        self._update_above(L)
        self._update_above(R)

    """
    始点 l を固定
    f(x_l*...*x_{r-1}) が True になる最大の r 
    つまり TTTTFFFF となるとき、F となる最小の添え字
    存在しない場合 n が返る
    f(e_M) = True でないと壊れる
    """
    def max_right(self, l, g):
        if l == self.N: return self.N
        l += self.N0
        l //= (l&-l)
        self._propagate_above(l)
        sm = self.e_X
        while True:
            if not g(self.op_X(sm, self.data[l])):
                while l < self.N0:
                    self.push(l)
                    l *= 2
                    if g(self.op_X(sm, self.data[l])):
                        sm = self.op_X(sm, self.data[l])
                        l += 1
                return l - self.N0
            sm = self.op_X(sm, self.data[l])
            l += 1
            if l&-l == l: break
        return self.N
 
    """
    終点 r を固定
    f(x_l*...*x_{r-1}) が True になる最小の l
    つまり FFFFTTTT となるとき、T となる最小の添え字
    存在しない場合 r が返る
    f(e_M) = True でないと壊れる
    """
    def min_left(self, r, g):
        if r == 0: return 0
        r += self.N0
        for i in range(self.log, 0, -1): self.push((r-1)>>i)
        sm = self.e_X
        while True:
            r -= 1
            while r>1 and r&1:
                r >>= 1
            if not g(self.op_X(self.data[r], sm)):
                while r < self.N0:
                    self.push(r)
                    r = 2*r + 1
                    if g(self.op_X(self.data[r], sm)):
                        sm = self.op_X(self.data[r], sm)
                        r -= 1
                return r + 1 - self.N0
            sm = self.op_X(self.data[r], sm)
            if r&-r == r: break
        return 0

###################################################################
#
###################################################################


class RangeAddRangeMin(LazySegmentTreeCommutative):
    def __init__(self,N,MAX,array=None):
        from operator import add
        super().__init__(min, MAX, add, add, 0, N, array)

import sys
readline = sys.stdin.readline

n, = map(int, readline().split())
*a, = map(int, readline().split())
q, = map(int, readline().split())

seg = RangeAddRangeMin(n,1<<60,a)

for _ in range(q):
    k,l,r,c = map(int, readline().split())
    if k==1:
        seg.apply(l-1,r,c)
    else:
        print(seg.prod(l-1,r))

    
    #print(seg)
0