結果

問題 No.1110 好きな歌
ユーザー 双六
提出日時 2020-08-26 07:08:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 625 ms / 5,000 ms
コード長 1,579 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 82,368 KB
実行使用メモリ 146,920 KB
最終ジャッジ日時 2024-11-06 19:58:10
合計ジャッジ時間 18,937 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

class SegmentTree(object):
	def __init__(self, N):
		self.N = N
		self.N0 = 2 ** (N - 1).bit_length()
		self.initVal = 0
		self.data = [self.initVal] * (2 * self.N0)

	def calc(self, a, b):
		return a + b

	def add(self, k, x):
		k += self.N0 - 1
		self.data[k] += x
		while k > 0:
			k = (k - 1) // 2
			self.data[k] = self.calc(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 = self.initVal
		while L < R:
			if R & 1:
				R -= 1
				m = self.calc(m, self.data[R - 1])
			if L & 1:
				m = self.calc(m, self.data[L - 1])
				L += 1
			L >>= 1; R >>= 1

		return m

def Binary_Search(x, B, D, M):
	#初期化
	left = 0
	right = M
	itr = -INF
	
	#二分探索
	while left <= right:
		mid = (left + right) // 2
		if B[mid] <= x - D:
			itr = max(itr, mid)
			left = mid + 1
		else:
			right = mid - 1

	return itr

#処理内容
def main():
	N, D = getlist()
	A = []
	for i in range(N):
		a = int(input())
		A.append(a)

	B = list(set(A))
	B.sort()
	compress = defaultdict(int)
	M = len(B)
	# 元の座標→0-indexed
	for i in range(M):
		compress[B[i]] = i

	# 前処理
	Seg = SegmentTree(N)
	for i in range(N):
		itr = compress[A[i]]
		Seg.add(itr, 1)
	
	for i in range(N):
		itr = Binary_Search(A[i], B, D, M)
		if itr == -INF:
			print(0)
		else:
			ans = Seg.query(0, itr)
			print(ans)

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