結果

問題 No.59 鉄道の旅
ユーザー nebukuro09nebukuro09
提出日時 2016-10-27 16:25:14
言語 PyPy2
(7.3.15)
結果
WA  
実行時間 -
コード長 1,692 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 77,524 KB
実行使用メモリ 109,892 KB
最終ジャッジ日時 2023-08-26 07:21:54
合計ジャッジ時間 5,632 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
98,296 KB
testcase_01 AC 122 ms
98,092 KB
testcase_02 AC 123 ms
98,212 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 218 ms
107,812 KB
testcase_12 AC 567 ms
108,796 KB
testcase_13 WA -
testcase_14 AC 170 ms
107,468 KB
testcase_15 AC 125 ms
98,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:
    class Node:
        def __init__(self, l, r, n):
            self.l = l
            self.r = r
            self.n = n
            self.left_child = None
            self.right_child = None

        def update(self, i, n):
            if self.l <= i < self.r:
                self.n += n
                if self.right_child is None:
                    return
                if i < self.l+(self.r-self.l)/2:
                    self.left_child.update(i, n)
                else:
                    self.right_child.update(i, n)

        def get_sum(self, l, r):
            ret = 0
            if r <= self.l or self.r <= l:
                return 0
            elif l <= self.l and self.r <= r:
                return self.n
            else:
                return self.left_child.get_sum(l, r) + self.right_child.get_sum(l, r)
            
    def __init__(self, n):
        m = 1
        while m < n:
            m *= 2
        self.root = self._construct(0, m)

    def _construct(self, l, r):
        node = self.Node(l, r, 0)
        if r-l <= 1:
            return node
        mid = l+(r-l)/2
        node.left_child = self._construct(l, mid)
        node.right_child = self._construct(mid, r)
        return node

    def update(self, i, n):
        self.root.update(i, n)
    
    def get_sum(self, l, r):
        return self.root.get_sum(l, r)

W_MAX = 100001
N, K = map(int, raw_input().split())
sg = SegmentTree(W_MAX)
ans = 0
for _ in xrange(N):
    w = int(raw_input())
    if w >= 0 and sg.get_sum(w, W_MAX) < K:
        ans += 1
        sg.update(w, 1)
    elif w < 0 and sg.get_sum(-w, -w+1) > 0:
        ans -= 1
        sg.update(w, -1)
print ans
0