結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
60,344 KB
testcase_01 AC 46 ms
60,592 KB
testcase_02 AC 45 ms
60,068 KB
testcase_03 AC 48 ms
60,696 KB
testcase_04 AC 175 ms
87,580 KB
testcase_05 AC 45 ms
60,416 KB
testcase_06 AC 53 ms
66,048 KB
testcase_07 AC 47 ms
60,160 KB
testcase_08 AC 91 ms
83,932 KB
testcase_09 AC 89 ms
84,164 KB
testcase_10 AC 99 ms
84,596 KB
testcase_11 AC 78 ms
83,764 KB
testcase_12 AC 124 ms
84,056 KB
testcase_13 AC 157 ms
89,112 KB
testcase_14 AC 172 ms
94,324 KB
testcase_15 AC 45 ms
60,796 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 -= 1
print(bit.sum(10**6+5)) 
0