結果
問題 | No.59 鉄道の旅 |
ユーザー |
![]() |
提出日時 | 2020-12-25 18:52:32 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 123 ms / 5,000 ms |
コード長 | 1,281 bytes |
コンパイル時間 | 605 ms |
コンパイル使用メモリ | 82,280 KB |
実行使用メモリ | 84,056 KB |
最終ジャッジ日時 | 2024-09-22 12:18:28 |
合計ジャッジ時間 | 2,155 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 12 |
ソースコード
import sysinput = sys.stdin.buffer.readlinesys.setrecursionlimit(10 ** 7)class fenwick_tree(object):def __init__(self, n):self.n = nself.log = n.bit_length()self.data = [0] * ndef __sum(self, r):s = 0while r > 0:s += self.data[r - 1]r -= r & -rreturn sdef add(self, p, x):""" a[p] += xを行う"""p += 1while p <= self.n:self.data[p - 1] += xp += p & -pdef sum(self, l, r):"""a[l] + a[l+1] + .. + a[r-1]を返す"""return self.__sum(r) - self.__sum(l)def lower_bound(self, x):"""a[0] + a[1] + .. a[i] >= x となる最小のiを返す"""if x <= 0:return -1i = 0k = 1 << self.logwhile k:if i + k <= self.n and self.data[i + k - 1] < x:x -= self.data[i + k - 1]i += kk >>= 1return iU = 10 ** 6 + 100N, K = map(int, input().split())bit = fenwick_tree(U)for _ in range(N):w = int(input())if w > 0:if bit.sum(w, U) < K:bit.add(w, 1)else:w = -wif bit.sum(w, w + 1):bit.add(w, -1)print(bit.sum(0, U))