結果

問題 No.1441 MErGe
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-08-09 14:31:57
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,489 bytes
コンパイル時間 1,159 ms
コンパイル使用メモリ 81,184 KB
実行使用メモリ 116,624 KB
最終ジャッジ日時 2023-10-21 12:17:16
合計ジャッジ時間 19,945 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,164 KB
testcase_01 AC 38 ms
52,200 KB
testcase_02 AC 37 ms
52,124 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 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 331 ms
116,300 KB
testcase_29 AC 370 ms
116,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#BIT
class BinaryIndexedTree():
    def __init__(self, n: int) -> None:
        self.n = 1 << (n.bit_length())
        self.BIT = [0] * (self.n + 1)

    def build(self, init_lis: list) -> None:
        for i, v in enumerate(init_lis):
            self.add(i, v)

    def add(self, i: int, x: int) -> None:
        i += 1
        while i <= self.n:
            self.BIT[i] += x
            i += i & -i
    
    def sum(self, l: int, r: int) -> int:
        return self._sum(r) - self._sum(l)

    def _sum(self, i: int) -> int:
        res = 0
        while i > 0:
            res += self.BIT[i]
            i -= i & -i
        return res

    def binary_search(self, x: int) -> int:
        i = self.n
        while True:
            if i & 1:
                if x > self.BIT[i]:
                    i += 1
                break
            if x > self.BIT[i]:
                x -= self.BIT[i]
                i += (i & -i) >> 1
            else:
                i -= (i & -i) >> 1
        return i

n, q = map(int, input().split())
a = list(map(int, input().split()))
from itertools import accumulate
a = [0] + list(accumulate(a))
BIT = BinaryIndexedTree(n)
for i in range(1, n + 1):
    BIT.add(i, 1)
for _ in range(q):
    t, l, r = map(int, input().split())
    if t == 1:
        for _ in range(r - l): 
            i = BIT.binary_search(l + 1)
            BIT.add(i, -1)
    else:
        L =  BIT.binary_search(l)
        R = BIT.binary_search(r)
        print(a[R - 1] - a[L - 2])
0