結果

問題 No.865 24時間降水量
ユーザー lloyzlloyz
提出日時 2024-05-10 22:55:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,661 ms / 2,000 ms
コード長 4,321 bytes
コンパイル時間 142 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 110,792 KB
最終ジャッジ日時 2024-05-10 22:55:24
合計ジャッジ時間 8,090 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,356 KB
testcase_01 AC 33 ms
53,024 KB
testcase_02 AC 34 ms
53,760 KB
testcase_03 AC 32 ms
54,680 KB
testcase_04 AC 33 ms
53,756 KB
testcase_05 AC 86 ms
76,696 KB
testcase_06 AC 84 ms
76,744 KB
testcase_07 AC 85 ms
76,784 KB
testcase_08 AC 92 ms
77,252 KB
testcase_09 AC 86 ms
76,852 KB
testcase_10 AC 157 ms
77,332 KB
testcase_11 AC 153 ms
77,056 KB
testcase_12 AC 155 ms
77,028 KB
testcase_13 AC 157 ms
77,312 KB
testcase_14 AC 161 ms
77,420 KB
testcase_15 AC 1,624 ms
110,792 KB
testcase_16 AC 1,616 ms
110,304 KB
testcase_17 AC 1,661 ms
110,528 KB
testcase_18 AC 34 ms
53,080 KB
testcase_19 AC 35 ms
53,960 KB
testcase_20 AC 33 ms
53,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:

    __all__ = ['setval', 'pointupdate', 'segquery', 'segsearch_right', 'pointgetval']

    def __init__(self, n=10**6, idetify_elt=-10**9, func=max):
        assert (func(idetify_elt, idetify_elt) == idetify_elt)
        self._n = n
        self._seg_length_half = 2**(n-1).bit_length()
        self._idetify_elt = idetify_elt
        self._seg = [idetify_elt]*(2*self._seg_length_half)
        self._func = func

    def setval(self, x_list):
        '''Set value : A = x_list'''
        assert (len(x_list) == self._n)
        # Set value at the bottom
        for i in range(self._n):
            self._seg[i+self._seg_length_half-1] = x_list[i]    
        # Build value
        for i in range(self._seg_length_half-2, -1, -1):
            self._seg[i] = self._func(self._seg[2*i+1], self._seg[2*i+2])
    
    def pointupdate(self, k, x):
        '''Update : A[k] = x '''
        pos = k + self._seg_length_half - 1
        # Set value at k-th
        self._seg[pos] = x
        # Build bottom-up
        while pos:
            pos = (pos-1)//2
            self._seg[pos] = self._func(self._seg[pos*2+1], self._seg[pos*2+2])
    
    def pointgetval(self, k):
        ''' Return A[k] '''
        return self._seg[k + self._seg_length_half - 1]

    def segquery(self, left, right):
        ''' Return func(A[left], ... , A[right-1]) '''
        # if not left < right
        if right <= left:
            return self._idetify_elt
        
        func_value = self._idetify_elt
        leftpos = left + self._seg_length_half - 1 # leftmost segment
        rightpos = right + self._seg_length_half - 2 # rightmost segment

        while leftpos < rightpos-1:
            if leftpos&1 == 0:
                # if leftpos is right-child
                func_value = self._func(func_value, self._seg[leftpos])
            if rightpos&1 == 1:
                # if rightpos is leftchild
                func_value = self._func(func_value, self._seg[rightpos])
                rightpos -= 1
            # move up
            leftpos = leftpos//2
            rightpos = (rightpos-1)//2
        
        func_value = self._func(func_value, self._seg[leftpos])
        if leftpos != rightpos:
            func_value = self._func(func_value, self._seg[rightpos])
        return func_value

    def segsearch_right(self, condfunc, left=0):
        ''' Return min_i satisfying condfunc( func( A[left], ... , A[i])) 
        if impossible : return n
        '''
        # if impossible (ie. condfunc( func( A[left], ... , A[-1])) is False)
        if not condfunc(self._segquery(left, self._n)):
            return self._n
        
        # possible
        func_value = self._idetify_elt
        rightpos = left + self._seg_length_half - 1
        while True: 
            # while rightpos is the left-child, move bottom-up
            while rightpos&1 == 1:
                rightpos //= 2
            # try
            up_value_trial = self._func(func_value, self._seg[rightpos])
            if not condfunc(up_value_trial):
                # move up and right
                func_value = up_value_trial
                rightpos = (rightpos-1)//2 + 1
            else:
                # move top-down
                while rightpos < self._seg_length_half-1:
                    down_value_trial = self._func(func_value, self._seg[rightpos*2 + 1])
                    if condfunc(down_value_trial):
                        # move left-child
                        rightpos = rightpos*2 + 1
                    else:
                        # move right-child
                        func_value = down_value_trial
                        rightpos = rightpos*2 + 2
                return rightpos - self._seg_length_half + 1

n = int(input())
A = list(map(int, input().split()))
D = [0 for _ in range(n - 23)]
res = 0
for i in range(n):
    res += A[i]
    if i >= 23:
        D[i - 23] = res
        res -= A[i - 23]
tree = SegmentTree(n - 23, 0, max)
tree.setval(D)
q = int(input())
for _ in range(q):
    t, v = map(int, input().split())
    t -= 1
    A[t] = v
    res = 0
    num = 0
    for i in range(max(0, t - 23), min(n, t + 24)):
        res += A[i]
        num += 1
        if num >= 24:
            tree.pointupdate(i - 23, res)
            res -= A[i - 23]
    print(tree.segquery(0, n - 23))

0