結果

問題 No.59 鉄道の旅
ユーザー 双六双六
提出日時 2020-07-26 16:16:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 237 ms / 5,000 ms
コード長 1,168 bytes
コンパイル時間 1,848 ms
コンパイル使用メモリ 86,580 KB
実行使用メモリ 94,952 KB
最終ジャッジ日時 2023-09-11 02:26:47
合計ジャッジ時間 4,123 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
87,928 KB
testcase_01 AC 109 ms
87,980 KB
testcase_02 AC 108 ms
87,792 KB
testcase_03 AC 107 ms
88,020 KB
testcase_04 AC 237 ms
94,920 KB
testcase_05 AC 117 ms
93,148 KB
testcase_06 AC 119 ms
93,244 KB
testcase_07 AC 108 ms
88,000 KB
testcase_08 AC 156 ms
94,952 KB
testcase_09 AC 161 ms
94,896 KB
testcase_10 AC 154 ms
94,188 KB
testcase_11 AC 132 ms
94,016 KB
testcase_12 AC 157 ms
94,076 KB
testcase_13 AC 204 ms
94,844 KB
testcase_14 AC 205 ms
94,308 KB
testcase_15 AC 111 ms
87,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class SegmentTree(object):
	#N:処理する区間の長さ
	def __init__(self, N):
		self.N0 = 2 ** (N - 1).bit_length()
		self.data = [0] * (2 * self.N0)

	#k番目の値をxに更新
	def update(self, k, x):
		k += self.N0 - 1
		self.data[k] = x
		while k > 0:
			k = (k - 1) // 2
			self.data[k] = self.data[2 * k + 1] + self.data[2 * k + 2]

	# 区間和
	def query(self, l, r):
		L = l + self.N0; R = r + self.N0 + 1
		m = 0
		while L < R:
			if R & 1:
				R -= 1
				m += self.data[R - 1]
			if L & 1:
				m += self.data[L - 1]
				L += 1
			L >>= 1; R >>= 1

		return m

#処理内容
def main():
	N, K = getlist()
	Seg = SegmentTree(10 ** 6 + 1)
	for i in range(N):
		W = int(input())
		if W < 0:
			val = Seg.query(-W, -W)
			if val >= 1:
				Seg.update(-W, val - 1)

		else:
			val = Seg.query(W, 10 ** 6)
			if val < K:
				num = Seg.query(W, W)
				Seg.update(W, num + 1)


	ans = Seg.query(0, 10 ** 6)
	print(ans)

if __name__ == '__main__':
	main()
0