結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 229 ms
84,496 KB
testcase_01 AC 39 ms
52,864 KB
testcase_02 AC 37 ms
52,608 KB
testcase_03 AC 37 ms
53,376 KB
testcase_04 AC 36 ms
52,864 KB
testcase_05 AC 36 ms
52,736 KB
testcase_06 AC 36 ms
52,864 KB
testcase_07 AC 37 ms
53,120 KB
testcase_08 AC 36 ms
52,820 KB
testcase_09 AC 35 ms
53,120 KB
testcase_10 AC 256 ms
84,776 KB
testcase_11 AC 296 ms
88,668 KB
testcase_12 AC 190 ms
82,132 KB
testcase_13 AC 176 ms
79,780 KB
testcase_14 AC 260 ms
88,632 KB
testcase_15 AC 197 ms
83,748 KB
testcase_16 AC 154 ms
83,932 KB
testcase_17 AC 191 ms
80,364 KB
testcase_18 AC 315 ms
85,628 KB
testcase_19 AC 159 ms
83,696 KB
testcase_20 AC 81 ms
78,972 KB
testcase_21 AC 272 ms
83,296 KB
testcase_22 AC 203 ms
87,676 KB
testcase_23 AC 170 ms
84,212 KB
testcase_24 AC 206 ms
86,976 KB
testcase_25 AC 289 ms
85,796 KB
testcase_26 AC 179 ms
86,320 KB
testcase_27 AC 237 ms
84,508 KB
testcase_28 AC 228 ms
81,848 KB
testcase_29 AC 232 ms
83,632 KB
testcase_30 AC 268 ms
99,208 KB
testcase_31 AC 208 ms
84,632 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 initialize(self, A):
        for i, a in enumerate(A, 1):
            self.tree[i] = a
            j = (i & -i) >> 1
            while j:
                self.tree[i] += self.tree[i - j]
                j >>= 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)
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