結果

問題 No.59 鉄道の旅
ユーザー 👑 rin204rin204
提出日時 2022-07-04 17:09:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 195 ms / 5,000 ms
コード長 1,167 bytes
コンパイル時間 830 ms
コンパイル使用メモリ 87,100 KB
実行使用メモリ 85,688 KB
最終ジャッジ日時 2023-08-21 00:54:05
合計ジャッジ時間 3,847 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
79,212 KB
testcase_01 AC 74 ms
79,356 KB
testcase_02 AC 73 ms
79,296 KB
testcase_03 AC 75 ms
79,232 KB
testcase_04 AC 195 ms
85,596 KB
testcase_05 AC 75 ms
83,520 KB
testcase_06 AC 82 ms
83,596 KB
testcase_07 AC 78 ms
83,520 KB
testcase_08 AC 118 ms
85,456 KB
testcase_09 AC 124 ms
85,688 KB
testcase_10 AC 126 ms
85,592 KB
testcase_11 AC 112 ms
85,336 KB
testcase_12 AC 159 ms
85,424 KB
testcase_13 AC 181 ms
85,412 KB
testcase_14 AC 174 ms
85,468 KB
testcase_15 AC 78 ms
79,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    def __init__(self, n):
        self.size = n
        self.n0 = 1 << (n.bit_length() - 1)
        self.tree = [0] * (n + 1)
    
    def range_sum(self, l, r):
        return self.sum(r - 1) - self.sum(l - 1)
        
    def sum(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
        
    def get(self, i):
        return self.sum(i) - self.sum(i - 1)
 
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
         
    def lower_bound(self, x):
        pos = 0
        plus = self.n0
        while plus > 0:
            if pos + plus <= self.size and self.tree[pos + plus] < x:
                x -= self.tree[pos + plus]
                pos += plus
            plus //= 2
        return pos

n, k = map(int, input().split())
m = 10 ** 6 + 1
bit = Bit(m)
for _ in range(n):
    w = int(input())
    if w > 0:
        cnt = bit.range_sum(w, m)
        if cnt < k:
            bit.add(w, 1)
    else:
        w *= -1
        if bit.get(w) >= 1:
            bit.add(w, -1)
print(bit.range_sum(0, m))
0