結果

問題 No.59 鉄道の旅
ユーザー rlangevinrlangevin
提出日時 2023-08-09 08:58:40
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,242 bytes
コンパイル時間 549 ms
コンパイル使用メモリ 82,024 KB
実行使用メモリ 79,352 KB
最終ジャッジ日時 2024-04-26 23:31:27
合計ジャッジ時間 2,459 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,224 KB
testcase_01 AC 39 ms
53,932 KB
testcase_02 AC 40 ms
53,544 KB
testcase_03 AC 39 ms
54,120 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 80 ms
79,352 KB
testcase_12 AC 129 ms
79,056 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Fenwick_Tree:
    def __init__(self, n):
        self._n = n
        self.data = [0] * n
 
    def add(self, p, x):
        assert 0 <= p < self._n
        p += 1
        while p <= self._n:
            self.data[p - 1] += x
            p += p & -p
 
    def sum(self, l, r):
        assert 0 <= l <= r <= self._n
        return self._sum(r) - self._sum(l)
 
    def _sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s
    
    def get(self, k):
        k += 1
        x, r = 0, 1
        while r < self._n:
            r <<= 1
        len = r
        while len:
            if x + len - 1 < self._n:
                if self.data[x + len - 1] < k:
                    k -= self.data[x + len - 1]
                    x += len
            len >>= 1
        return x
    

N, K = map(int, input().split())
M = 10 ** 5 + 5
T = Fenwick_Tree(M)
L = [0] * M
for i in range(N):
    w = int(input())
    if w > 0:
        if T.sum(w, M) >= K:
            continue
        else:
            L[w] += 1
            T.add(w, 1)
    else:
        w = -w
        if L[w] == 0:
            continue
        else:
            L[w] -= 1
            T.add(w, -1)

print(T.sum(0, M))
0