結果
| 問題 |
No.1332 Range Nearest Query
|
| コンテスト | |
| ユーザー |
convexineq
|
| 提出日時 | 2021-01-09 15:52:02 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 2,388 ms / 2,500 ms |
| コード長 | 2,000 bytes |
| コンパイル時間 | 411 ms |
| コンパイル使用メモリ | 82,732 KB |
| 実行使用メモリ | 212,460 KB |
| 最終ジャッジ日時 | 2025-03-17 10:14:16 |
| 合計ジャッジ時間 | 73,735 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 48 |
ソースコード
class segment_tree:
__slots__ = ["op_M", "e_M","N","N0","dat"]
def __init__(self, N, operator_M, e_M):
self.op_M = operator_M
self.e_M = e_M
self.N = N
self.N0 = 1<<(N-1).bit_length()
self.dat = [self.e_M]*(2*self.N0)
# 長さNの配列 initial で初期化
def build(self, initial):
assert self.N == len(initial)
self.dat[self.N0:self.N0+len(initial)] = initial[:]
for k in range(self.N0-1,0,-1):
self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1])
# a_k の値を x に更新
def update(self,k,x):
k += self.N0
self.dat[k] = x
k >>= 1
while k:
self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1])
k >>= 1
# 区間[L,R]をopでまとめる
def query(self,L,R):
L += self.N0; R += self.N0 + 1
sl = sr = self.e_M
while L < R:
if R & 1:
R -= 1
sr = self.op_M(self.dat[R],sr)
if L & 1:
sl = self.op_M(sl,self.dat[L])
L += 1
L >>= 1; R >>= 1
return self.op_M(sl,sr)
def get(self, k): #k番目の値を取得。query[k,k]と同じ
return self.dat[k+self.N0]
n = int(input())
*a, = map(int,input().split())
def merge(a,b):
return sorted(a+b)
seg = segment_tree(n, merge, [])
seg.build([[ai] for ai in a])
def getans(lst,x):
v = INF
idx = bisect_left(lst,x)
if idx < len(lst):
v = lst[idx]-x
if idx:
v = min(v,x-lst[idx-1])
return v
INF = 10**9
from bisect import bisect_left, bisect_right
Q = int(input())
N0 = seg.N0
for _ in range(Q):
L,R,x = map(int,input().split())
L += N0-1; R += N0
ans = INF
while L < R:
if R & 1:
R -= 1
ans = min(ans,getans(seg.dat[R],x))
if L & 1:
ans = min(ans,getans(seg.dat[L],x))
L += 1
L >>= 1; R >>= 1
print(ans)
convexineq