結果

問題 No.59 鉄道の旅
コンテスト
ユーザー nebukuro09
提出日時 2016-10-27 16:25:14
言語 PyPy2
(7.3.15)
結果
WA  
実行時間 -
コード長 1,692 bytes
コンパイル時間 2,546 ms
コンパイル使用メモリ 76,288 KB
実行使用メモリ 107,352 KB
最終ジャッジ日時 2024-12-24 23:14:06
合計ジャッジ時間 6,220 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 4 WA * 8
権限があれば一括ダウンロードができます

ソースコード

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