結果

問題 No.59 鉄道の旅
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-01-01 17:32:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,072 bytes
コンパイル時間 535 ms
コンパイル使用メモリ 82,216 KB
実行使用メモリ 94,316 KB
最終ジャッジ日時 2024-04-18 15:22:16
合計ジャッジ時間 2,869 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
59,904 KB
testcase_01 AC 39 ms
59,904 KB
testcase_02 AC 40 ms
60,032 KB
testcase_03 AC 39 ms
59,904 KB
testcase_04 AC 145 ms
88,072 KB
testcase_05 AC 42 ms
60,160 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 72 ms
83,968 KB
testcase_12 AC 110 ms
84,008 KB
testcase_13 AC 139 ms
89,168 KB
testcase_14 AC 153 ms
94,316 KB
testcase_15 AC 38 ms
59,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
 
    def build(self, list):
        self.tree[1:] = list.copy()
        for i in range(self.size+1):
            j = i + (i & (-i))
            if j < self.size+1:
                self.tree[j] += self.tree[i]

    def sum(self, i):
        # [0, i) の要素の総和を返す
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s
    # 0 index を 1 index に変更  転倒数を求めるなら1を足していく
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i



n,k = map(int,input().split())
bit = BIT(10**6+5)
dic = {}
s = 0
for i in range(n):
    w = int(input())
    if w >= 0:
        if s - bit.sum(w) >= k:
            continue
        bit.add(w,1)
        s += 1
        dic[w] = dic.get(w,0)+1
    else:
        w *= -1
        if dic.get(w,0) > 0:
            bit.add(w,-1)
            dic[w] -= 1
            s -= 2
print(bit.sum(10**6+5)) 
0