結果

問題 No.59 鉄道の旅
ユーザー はむ吉🐹
提出日時 2016-09-15 17:17:16
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 707 ms / 5,000 ms
コード長 986 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 22,656 KB
最終ジャッジ日時 2024-12-24 23:08:37
合計ジャッジ時間 4,577 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3


class FenwickTree(object):

    def __init__(self, num_elems):
        self.num_elems = num_elems
        self.data = [0 for _ in range(num_elems)]

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

    def sum_range(self, start, end):
        return self.sum_to(end) - self.sum_to(start)

    def add(self, idx, x):
        while idx < self.num_elems:
            self.data[idx] += x
            idx |= idx + 1


def main():
    n, k = map(int, input().split())
    ws = [int(input()) for _ in range(n)]
    max_weight = max(map(abs, ws))
    ft = FenwickTree(max_weight + 1)
    for w in ws:
        if w > 0 and ft.sum_range(w, max_weight + 1) < k:
            ft.add(w, 1)
        elif w < 0 and ft.sum_range(abs(w), abs(w) + 1) > 0:
            ft.add(abs(w), -1)
    print(ft.sum_to(max_weight + 1))


if __name__ == '__main__':
    main()
0