結果

問題 No.776 A Simple RMQ Problem
ユーザー maspymaspy
提出日時 2020-05-01 12:15:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,690 ms / 3,000 ms
コード長 2,660 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 87,148 KB
実行使用メモリ 181,144 KB
最終ジャッジ日時 2023-08-25 11:14:37
合計ジャッジ時間 50,059 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,444 KB
testcase_01 AC 69 ms
71,376 KB
testcase_02 AC 682 ms
95,340 KB
testcase_03 AC 1,466 ms
128,544 KB
testcase_04 AC 1,563 ms
126,416 KB
testcase_05 AC 2,361 ms
164,396 KB
testcase_06 AC 1,191 ms
112,576 KB
testcase_07 AC 1,808 ms
142,748 KB
testcase_08 AC 1,967 ms
140,752 KB
testcase_09 AC 947 ms
114,172 KB
testcase_10 AC 1,491 ms
120,584 KB
testcase_11 AC 2,022 ms
156,760 KB
testcase_12 AC 2,444 ms
175,412 KB
testcase_13 AC 2,324 ms
173,364 KB
testcase_14 AC 2,335 ms
170,552 KB
testcase_15 AC 2,428 ms
173,172 KB
testcase_16 AC 2,472 ms
173,424 KB
testcase_17 AC 2,690 ms
149,984 KB
testcase_18 AC 1,928 ms
181,144 KB
testcase_19 AC 1,591 ms
155,568 KB
testcase_20 AC 1,714 ms
149,500 KB
testcase_21 AC 1,554 ms
157,720 KB
testcase_22 AC 1,599 ms
143,412 KB
testcase_23 AC 2,471 ms
157,136 KB
testcase_24 AC 1,869 ms
145,696 KB
testcase_25 AC 134 ms
88,084 KB
testcase_26 AC 1,720 ms
170,724 KB
testcase_27 AC 1,587 ms
173,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

class SegTree:
    def __init__(self, N, X_f, X_unit, X_commutative=False):
        self.N = N
        self.X_f = X_f
        self.X_unit = X_unit
        self.X = [self.X_unit] * (N + N)
        if X_commutative:
            self.fold = self._fold_commutative

    def build(self, seq):
        for i, x in enumerate(seq, self.N):
            self.X[i] = x
        for i in range(self.N - 1, 0, -1):
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def set_val(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def fold(self, L, R):
        L += self.N
        R += self.N
        vL = vR = self.X_unit
        while L < R:
            if L & 1:
                vL = self.X_f(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.X_f(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.X_f(vL, vR)

    def _fold_commutative(self, L, R):
        L += self.N
        R += self.N
        v = self.X_unit
        while L < R:
            if L & 1:
                v = self.X_f(v, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                v = self.X_f(v, self.X[R])
            L >>= 1
            R >>= 1
        return v

X_unit = None


def X_f(x, y):
    if x is None:
        return y
    if y is None:
        return x
    return (max(x[0], y[0], x[2] + y[1]), max(x[1], x[3] + y[1]),
            max(y[2], x[2] + y[3]), x[3] + y[3])

N, Q = map(int, readline().split())
A = map(int, readline().split())
seg = SegTree(N, X_f, X_unit)
seg.build((x, x, x, x) for x in A)

def range_max(L1, L2, R1, R2):
    R1 = max(L1, R1)
    L2 = min(L2, R2)
    if L2 < R1:
        mid = 0 if R1 == L2 + 1 else seg.fold(L2 + 1, R1)[3]
        return seg.fold(L1, L2 + 1)[2] + mid + seg.fold(R1, R2 + 1)[1]
    ret = -10**18
    # R1 より左に左端がある
    if L1 < R1:
        ret = max(ret, seg.fold(L1, R1)[2] + seg.fold(R1, R2 + 1)[1])
    # L2 以前に右端がある
    ret = max(ret, seg.fold(R1, L2 + 1)[0])
    # L2 より右に右端がある
    if L2 < R2:
        ret = max(ret, seg.fold(R1, L2 + 1)[2] + seg.fold(L2 + 1, R2 + 1)[1])
    return ret

for query in readlines():
    if query.startswith(b's'):
        i, x = map(int, query[4:].split())
        seg.set_val(i - 1, (x, x, x, x))
    else:
        print(range_max(*map(lambda x: int(x) - 1, query[4:].split())))
0