結果

問題 No.59 鉄道の旅
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-25 18:52:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 124 ms / 5,000 ms
コード長 1,281 bytes
コンパイル時間 509 ms
コンパイル使用メモリ 81,748 KB
実行使用メモリ 83,456 KB
最終ジャッジ日時 2023-10-23 18:29:53
合計ジャッジ時間 2,377 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
61,300 KB
testcase_01 AC 41 ms
61,300 KB
testcase_02 AC 40 ms
61,300 KB
testcase_03 AC 43 ms
61,300 KB
testcase_04 AC 124 ms
83,388 KB
testcase_05 AC 44 ms
67,136 KB
testcase_06 AC 45 ms
67,212 KB
testcase_07 AC 43 ms
67,132 KB
testcase_08 AC 66 ms
83,308 KB
testcase_09 AC 72 ms
83,400 KB
testcase_10 AC 72 ms
83,456 KB
testcase_11 AC 63 ms
83,336 KB
testcase_12 AC 88 ms
83,336 KB
testcase_13 AC 112 ms
83,448 KB
testcase_14 AC 107 ms
83,308 KB
testcase_15 AC 41 ms
61,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class fenwick_tree(object):
    def __init__(self, n):
        self.n = n
        self.log = n.bit_length()
        self.data = [0] * n

    def __sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def add(self, p, x):
        """ a[p] += xを行う"""
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        """a[l] + a[l+1] + .. + a[r-1]を返す"""
        return self.__sum(r) - self.__sum(l)

    def lower_bound(self, x):
        """a[0] + a[1] + .. a[i] >= x となる最小のiを返す"""
        if x <= 0:
            return -1
        i = 0
        k = 1 << self.log
        while k:
            if i + k <= self.n and self.data[i + k - 1] < x:
                x -= self.data[i + k - 1]
                i += k
            k >>= 1
        return i


U = 10 ** 6 + 100
N, K = map(int, input().split())
bit = fenwick_tree(U)
for _ in range(N):
    w = int(input())
    if w > 0:
        if bit.sum(w, U) < K:
            bit.add(w, 1)
    else:
        w = -w
        if bit.sum(w, w + 1):
            bit.add(w, -1)

print(bit.sum(0, U))
0