結果

問題 No.776 A Simple RMQ Problem
ユーザー maspymaspy
提出日時 2020-05-01 12:15:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,660 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 11,212 KB
実行使用メモリ 47,504 KB
最終ジャッジ日時 2023-08-25 11:11:39
合計ジャッジ時間 13,043 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
47,504 KB
testcase_01 AC 17 ms
8,508 KB
testcase_02 AC 466 ms
16,340 KB
testcase_03 AC 1,998 ms
33,288 KB
testcase_04 AC 2,766 ms
21,996 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

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