結果

問題 No.59 鉄道の旅
ユーザー nebukuro09nebukuro09
提出日時 2016-10-27 16:34:09
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 1,351 ms / 5,000 ms
コード長 1,822 bytes
コンパイル時間 917 ms
コンパイル使用メモリ 77,864 KB
実行使用メモリ 300,688 KB
最終ジャッジ日時 2023-08-26 07:41:31
合計ジャッジ時間 13,735 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 431 ms
286,036 KB
testcase_01 AC 430 ms
286,252 KB
testcase_02 AC 432 ms
285,968 KB
testcase_03 AC 497 ms
295,172 KB
testcase_04 AC 1,351 ms
299,644 KB
testcase_05 AC 507 ms
295,196 KB
testcase_06 AC 520 ms
296,496 KB
testcase_07 AC 501 ms
295,240 KB
testcase_08 AC 716 ms
298,212 KB
testcase_09 AC 663 ms
298,000 KB
testcase_10 AC 705 ms
299,476 KB
testcase_11 AC 629 ms
298,136 KB
testcase_12 AC 1,046 ms
299,064 KB
testcase_13 AC 1,161 ms
300,116 KB
testcase_14 AC 1,247 ms
300,688 KB
testcase_15 AC 436 ms
286,168 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 = 1000001
N, K = map(int, raw_input().split())
sg = SegmentTree(W_MAX)
ans = 0
d = {}
for _ in xrange(N):
    w = int(raw_input())
    #if w in d:
    #    d[w] += 1
    #else:
    #    d[w] = 1
    if w >= 0 and sg.get_sum(w, W_MAX) < K:
        ans += 1
        sg.update(w, 1)
        #print d
        #print w, sg.get_sum(w, W_MAX)
    elif w < 0 and sg.get_sum(-w, -w+1) > 0:
        ans -= 1
        sg.update(-w, -1)

print ans
0