結果

問題 No.1441 MErGe
ユーザー H3PO4H3PO4
提出日時 2022-05-26 09:32:23
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,795 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 81,876 KB
実行使用メモリ 112,048 KB
最終ジャッジ日時 2023-10-20 19:23:19
合計ジャッジ時間 12,721 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,528 KB
testcase_01 AC 38 ms
53,528 KB
testcase_02 AC 40 ms
53,528 KB
testcase_03 AC 82 ms
76,460 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 267 ms
108,572 KB
testcase_29 AC 272 ms
108,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import itertools


input = sys.stdin.buffer.readline


class Bit:
    """https://ikatakos.com/pot/programming_algorithm/data_structure/binary_indexed_tree から拝借しています。"""

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

    def __getitem__(self, item):
        return self.sum(item) - self.sum(item - 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 add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def lower_bound(self, x):
        """ 累積和がx以上になる最小のindexと、その直前までの累積和 """
        sum_ = 0
        pos = 0
        for i in range(self.depth, -1, -1):
            k = pos + (1 << i)
            if k <= self.size and sum_ + self.tree[k] < x:
                sum_ += self.tree[k]
                pos += 1 << i
        return pos + 1


N, Q = map(int, input().split())
A = tuple(map(int, input().split()))
As = [0] + list(itertools.accumulate(A))
bit = Bit(N + 1)
bit.initialize([0] + [1] * N)
for _ in range(Q):
    t, l, r = map(int, input().split())
    if t == 1:
        indices = []
        for i in range(l, r):
            indices.append(bit.lower_bound(i))
        for idx in indices:
            bit.add(idx + 1, -1)
    else:
        assert t == 2
        lidx = bit.lower_bound(l) - 1
        ridx = bit.lower_bound(r + 1) - 1
        print(As[ridx - 1] - As[lidx - 1])
0