結果

問題 No.833 かっこいい電車
ユーザー H3PO4H3PO4
提出日時 2021-02-27 12:24:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 566 ms / 2,000 ms
コード長 2,764 bytes
コンパイル時間 482 ms
コンパイル使用メモリ 82,696 KB
実行使用メモリ 99,536 KB
最終ジャッジ日時 2024-04-10 15:35:16
合計ジャッジ時間 12,256 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 407 ms
85,168 KB
testcase_01 AC 40 ms
52,992 KB
testcase_02 AC 40 ms
52,736 KB
testcase_03 AC 41 ms
53,356 KB
testcase_04 AC 40 ms
52,736 KB
testcase_05 AC 39 ms
52,992 KB
testcase_06 AC 40 ms
52,864 KB
testcase_07 AC 41 ms
53,504 KB
testcase_08 AC 39 ms
52,736 KB
testcase_09 AC 37 ms
52,688 KB
testcase_10 AC 483 ms
86,208 KB
testcase_11 AC 566 ms
90,444 KB
testcase_12 AC 325 ms
82,836 KB
testcase_13 AC 308 ms
81,640 KB
testcase_14 AC 478 ms
89,836 KB
testcase_15 AC 343 ms
83,520 KB
testcase_16 AC 263 ms
85,212 KB
testcase_17 AC 392 ms
80,656 KB
testcase_18 AC 547 ms
86,908 KB
testcase_19 AC 282 ms
84,396 KB
testcase_20 AC 104 ms
78,160 KB
testcase_21 AC 514 ms
83,680 KB
testcase_22 AC 338 ms
88,528 KB
testcase_23 AC 311 ms
86,084 KB
testcase_24 AC 355 ms
88,392 KB
testcase_25 AC 539 ms
85,796 KB
testcase_26 AC 313 ms
87,116 KB
testcase_27 AC 426 ms
85,024 KB
testcase_28 AC 407 ms
82,320 KB
testcase_29 AC 415 ms
84,764 KB
testcase_30 AC 374 ms
99,536 KB
testcase_31 AC 399 ms
84,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

    def initialize(self, A):
        for i, a in enumerate(A, 1):
            s = a
            lim = i & -i
            j = 1
            while j < lim:
                s += self.tree[i - j]
                j <<= 1
            self.tree[i] = s

    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)
bit.initialize(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