結果

問題 No.1441 MErGe
ユーザー H3PO4H3PO4
提出日時 2021-03-26 22:22:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,739 bytes
コンパイル時間 1,230 ms
コンパイル使用メモリ 86,776 KB
実行使用メモリ 187,536 KB
最終ジャッジ日時 2023-08-19 08:52:18
合計ジャッジ時間 19,296 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
187,536 KB
testcase_01 AC 73 ms
71,532 KB
testcase_02 AC 73 ms
71,328 KB
testcase_03 WA -
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 TLE -
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 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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)
bit.initialize([1] * N)
for _ in range(Q):
    t, l, r = map(int, input().split())
    if t == 1:
        lidx = bit.lower_bound(l)
        ridx = bit.lower_bound(r)
        for i in range(lidx + 1, ridx + 1):
            bit.add(i, -bit[i])
    else:  # t==2
        lidx = bit.lower_bound(l)
        ridx = bit.lower_bound(r)
        print(As[ridx] - As[lidx - 1])
0