結果

問題 No.833 かっこいい電車
ユーザー H3PO4H3PO4
提出日時 2021-02-19 10:14:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 2,596 bytes
コンパイル時間 1,093 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 103,876 KB
最終ジャッジ日時 2023-10-13 21:27:09
合計ジャッジ時間 11,215 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 282 ms
88,628 KB
testcase_01 AC 74 ms
71,420 KB
testcase_02 AC 75 ms
71,352 KB
testcase_03 AC 76 ms
71,572 KB
testcase_04 AC 73 ms
71,360 KB
testcase_05 AC 74 ms
71,116 KB
testcase_06 AC 73 ms
71,416 KB
testcase_07 AC 74 ms
71,116 KB
testcase_08 AC 71 ms
71,608 KB
testcase_09 AC 72 ms
71,268 KB
testcase_10 AC 341 ms
89,092 KB
testcase_11 AC 384 ms
95,120 KB
testcase_12 AC 250 ms
85,712 KB
testcase_13 AC 234 ms
81,652 KB
testcase_14 AC 355 ms
94,088 KB
testcase_15 AC 262 ms
87,740 KB
testcase_16 AC 217 ms
88,348 KB
testcase_17 AC 252 ms
80,452 KB
testcase_18 AC 372 ms
89,580 KB
testcase_19 AC 216 ms
87,232 KB
testcase_20 AC 118 ms
80,412 KB
testcase_21 AC 356 ms
85,852 KB
testcase_22 AC 278 ms
93,336 KB
testcase_23 AC 232 ms
87,896 KB
testcase_24 AC 287 ms
93,644 KB
testcase_25 AC 377 ms
89,500 KB
testcase_26 AC 243 ms
90,996 KB
testcase_27 AC 312 ms
88,348 KB
testcase_28 AC 287 ms
85,204 KB
testcase_29 AC 306 ms
86,740 KB
testcase_30 AC 341 ms
103,876 KB
testcase_31 AC 277 ms
88,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.buffer.readline


class Bit:
    """1-indexed"""

    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def _sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def sum(self, i, j):
        """閉区間[i, j]"""
        return self._sum(j) - self._sum(i - 1)

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


class SegmentTree:
    """
    https://tjkendev.github.io/procon-library/python/range_query/ruq_segment_tree.html から拝借しています。
    """

    # N: 処理する区間の長さ
    def __init__(self, N):
        self.N = N
        self.N0 = 2 ** (self.N - 1).bit_length()
        self.data = [None] * (2 * self.N0)
        self.INF = (-1, 2 ** 31 - 1)

    # 区間[l, r)の値をvに書き換える
    # vは(t, value)というタプルにする (新しい値ほどtは大きくなる)
    def update(self, l, r, v):
        L = l + self.N0
        R = r + self.N0
        while L < R:
            if R & 1:
                R -= 1
                self.data[R - 1] = v

            if L & 1:
                self.data[L - 1] = v
                L += 1
            L >>= 1
            R >>= 1

    # a_iの現在の値を取得
    def _query(self, k):
        k += self.N0 - 1
        s = self.INF
        while k >= 0:
            if self.data[k]:
                s = max(s, self.data[k])
            k = (k - 1) // 2
        return s

    # これを呼び出す
    def query(self, k):
        return self._query(k)[1]


N, Q = map(int, input().split())
A = map(int, input().split())

bit = Bit(N)
for i, a in enumerate(A, 1):
    bit.add(i, a)

seg_left = SegmentTree(N)
seg_right = SegmentTree(N)
t = 0
for i in range(1, N + 1):
    seg_left.update(i, i + 1, (t, i))
    seg_right.update(i, i + 1, (t, i))
    t += 1

for _ in range(Q):
    q, x = map(int, input().split())
    if q == 1:
        new_left = seg_left.query(x)
        new_right = seg_right.query(x + 1)
        seg_left.update(x + 1, new_right + 1, (t, new_left))
        seg_right.update(new_left, x + 1, (t, new_right))
        t += 1
    elif q == 2:
        left = seg_left.query(x)
        right = seg_right.query(x + 1)
        seg_left.update(x + 1, right + 1, (t, x + 1))
        seg_right.update(left, x + 1, (t, x))
        t += 1
    elif q == 3:
        bit.add(x, 1)
    else:  # q==4
        left = seg_left.query(x)
        right = seg_right.query(x)
        print(bit.sum(left, right))
0