結果

問題 No.1332 Range Nearest Query
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-14 10:43:40
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,684 bytes
コンパイル時間 1,472 ms
コンパイル使用メモリ 86,616 KB
実行使用メモリ 196,508 KB
最終ジャッジ日時 2023-09-28 21:45:56
合計ジャッジ時間 74,495 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,140 KB
testcase_01 AC 92 ms
71,356 KB
testcase_02 AC 84 ms
71,356 KB
testcase_03 AC 2,452 ms
175,328 KB
testcase_04 AC 2,303 ms
176,796 KB
testcase_05 AC 2,293 ms
176,672 KB
testcase_06 AC 1,171 ms
190,824 KB
testcase_07 AC 1,156 ms
191,084 KB
testcase_08 AC 1,152 ms
196,508 KB
testcase_09 AC 1,172 ms
190,912 KB
testcase_10 AC 1,143 ms
191,012 KB
testcase_11 AC 1,148 ms
191,136 KB
testcase_12 AC 1,178 ms
190,860 KB
testcase_13 AC 1,151 ms
190,864 KB
testcase_14 AC 1,182 ms
190,880 KB
testcase_15 AC 1,181 ms
191,000 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 AC 2,437 ms
193,828 KB
testcase_20 AC 2,464 ms
194,920 KB
testcase_21 AC 2,467 ms
195,352 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 AC 2,428 ms
193,572 KB
testcase_25 TLE -
testcase_26 AC 803 ms
186,748 KB
testcase_27 AC 821 ms
191,908 KB
testcase_28 AC 500 ms
104,552 KB
testcase_29 AC 531 ms
104,548 KB
testcase_30 AC 623 ms
105,504 KB
testcase_31 AC 389 ms
104,308 KB
testcase_32 AC 610 ms
105,216 KB
testcase_33 AC 636 ms
105,008 KB
testcase_34 AC 452 ms
104,156 KB
testcase_35 AC 517 ms
104,676 KB
testcase_36 AC 515 ms
104,768 KB
testcase_37 AC 542 ms
104,612 KB
testcase_38 AC 1,771 ms
138,136 KB
testcase_39 AC 1,047 ms
113,752 KB
testcase_40 TLE -
testcase_41 AC 1,248 ms
115,020 KB
testcase_42 AC 1,709 ms
141,628 KB
testcase_43 AC 1,354 ms
122,572 KB
testcase_44 AC 1,938 ms
157,096 KB
testcase_45 AC 1,883 ms
151,968 KB
testcase_46 AC 1,722 ms
133,980 KB
testcase_47 AC 1,824 ms
151,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline

class SegmentTree():
  def __init__(self,size,f=lambda x,y:x+y,default=0):
    self.size=pow(2,(size-1).bit_length())
    self.f=f
    self.default=default
    self.data=[default]*(self.size*2)
  # list[i]をxに更新
  def update(self,i,x):
    i+=self.size
    self.data[i]=x
    while i:
      i>>=1
      self.data[i]=self.f(self.data[i*2],self.data[i*2+1])
  # はじめの構築をO(N)で行う
  def init_build(self,ary):
    n=len(ary)
    for i in range(n):
      self.data[i+self.size]=ary[i]
    for i in reversed(range(self.size)):
      self.data[i]=self.f(self.data[i*2],self.data[i*2+1])
  # 区間[l,r)へのクエリ
  def query(self,l,r):
    l,r=l+self.size,r+self.size
    lret,rret=self.default,self.default
    while l<r:
      if l&1:
        lret=self.f(self.data[l],lret)
        l+=1
      if r&1:
        r-=1
        rret=self.f(self.data[r],rret)
      l>>=1
      r>>=1
    return self.f(lret,rret)
  def get(self,i):
    return self.data[self.size+i]



n=int(input())
xary=list(map(int,input().split()))
q=int(input())
lrx=[list(map(int,input().split())) for _ in range(q)]

events=[]
for i,x in enumerate(xary):
  events.append([x,i])
for i,(l,r,x) in enumerate(lrx):
  events.append([x,i+n])
events.sort(key=lambda x:x[0])

inf=float('inf')
mxst=SegmentTree(n,max,-inf)
mnst=SegmentTree(n,min,inf)
mnst.init_build(xary)

ret=[None]*q
idx=0
for x,i in events:
  if i<n:
    mxst.update(i,x)
    mnst.update(i,inf)
  else:
    l,r,x=lrx[i-n]
    y=mnst.query(l-1,r)
    z=mxst.query(l-1,r)
    tmp=inf
    if y!=inf:tmp=min(tmp,abs(x-y))
    if z!=-inf:tmp=min(tmp,abs(x-z))
    ret[i-n]=tmp
print(*ret,sep='\n')
0