結果
問題 | No.1332 Range Nearest Query |
ユーザー | titia |
提出日時 | 2021-01-14 05:33:29 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,346 bytes |
コンパイル時間 | 1,511 ms |
コンパイル使用メモリ | 86,216 KB |
実行使用メモリ | 131,252 KB |
最終ジャッジ日時 | 2023-08-15 10:26:45 |
合計ジャッジ時間 | 24,395 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 64 ms
71,420 KB |
testcase_01 | WA | - |
testcase_02 | AC | 65 ms
71,420 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 308 ms
130,880 KB |
testcase_07 | AC | 338 ms
130,836 KB |
testcase_08 | AC | 318 ms
130,824 KB |
testcase_09 | AC | 358 ms
130,976 KB |
testcase_10 | AC | 317 ms
130,804 KB |
testcase_11 | AC | 405 ms
130,852 KB |
testcase_12 | AC | 310 ms
130,796 KB |
testcase_13 | AC | 324 ms
130,856 KB |
testcase_14 | AC | 317 ms
130,884 KB |
testcase_15 | AC | 320 ms
131,112 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 233 ms
131,008 KB |
testcase_27 | AC | 273 ms
130,860 KB |
testcase_28 | AC | 212 ms
90,324 KB |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | AC | 173 ms
89,184 KB |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | AC | 198 ms
89,876 KB |
testcase_35 | AC | 219 ms
90,016 KB |
testcase_36 | AC | 220 ms
90,144 KB |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | WA | - |
testcase_44 | WA | - |
testcase_45 | WA | - |
testcase_46 | WA | - |
testcase_47 | WA | - |
ソースコード
import sys input = sys.stdin.readline N=int(input()) A=list(map(int,input().split())) Q=int(input()) Query=[tuple(map(int,input().split())) for i in range(Q)] def seg_function1(x,y): # Segment treeで扱うfunction return max(x,y) seg_el=1<<(N.bit_length()) # Segment treeの台の要素数 SEG1=[0]*(2*seg_el) # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化 for i in range(N): # Aを対応する箇所へupdate SEG1[i+seg_el]=A[i] for i in range(seg_el-1,0,-1): # 親の部分もupdate SEG1[i]=seg_function1(SEG1[i*2],SEG1[i*2+1]) def update1(n,x,seg_el): # A[n]をxへ更新(反映) i=n+seg_el SEG1[i]=x i>>=1 # 子ノードへ while i!=0: SEG1[i]=seg_function(SEG1[i*2],SEG1[i*2+1]) i>>=1 def getvalues1(l,r): # 区間[l,r)に関するseg_functionを調べる L=l+seg_el R=r+seg_el ANS=0 while L<R: if L & 1: ANS=seg_function1(ANS , SEG1[L]) L+=1 if R & 1: R-=1 ANS=seg_function1(ANS , SEG1[R]) L>>=1 R>>=1 return ANS def bisect_on_SEG1(l,x): # l以上で、x以上の最小のindexを返す。ただし、存在しない場合はNを返す。 L=l+seg_el R=seg_el*2-1 while L<R: if SEG1[L]>=x: break if L & 1: L+=1 L>>=1 R>>=1 if SEG1[L]<x: return N while L<seg_el: if SEG1[L*2]>=x: L=L*2 else: L=L*2+1 return L-seg_el def seg_function2(x,y): # Segment treeで扱うfunction return min(x,y) seg_el=1<<(N.bit_length()) # Segment treeの台の要素数 SEG2=[1<<30]*(2*seg_el) # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化 for i in range(N): # Aを対応する箇所へupdate SEG2[i+seg_el]=A[i] for i in range(seg_el-1,0,-1): # 親の部分もupdate SEG2[i]=seg_function2(SEG2[i*2],SEG2[i*2+1]) def update2(n,x,seg_el): # A[n]をxへ更新(反映) i=n+seg_el SEG2[i]=x i>>=1 # 子ノードへ while i!=0: SEG2[i]=seg_function2(SEG2[i*2],SEG2[i*2+1]) i>>=1 def getvalues2(l,r): # 区間[l,r)に関するseg_functionを調べる L=l+seg_el R=r+seg_el ANS=1<<30 while L<R: if L & 1: ANS=seg_function2(ANS , SEG2[L]) L+=1 if R & 1: R-=1 ANS=seg_function2(ANS , SEG2[R]) L>>=1 R>>=1 return ANS def bisect_on_SEG2(l,x): # l以上で、x以下の最小のindexを返す。ただし、存在しない場合はNを返す。 L=l+seg_el R=seg_el*2-1 while L<R: if SEG2[L]<=x: break if L & 1: L+=1 L>>=1 R>>=1 if SEG2[L]>x: return N while L<seg_el: if SEG2[L*2]<=x: L=L*2 else: L=L*2+1 return L-seg_el for l,r,x in Query: xx=bisect_on_SEG1(l-1,x) yy=bisect_on_SEG2(l-1,x) #print(xx,yy) ANS=1<<30 if l-1<=xx-1<=r-1: ANS=min(ANS,abs(getvalues1(l-1,xx-1)-x)) if l-1<=yy-1<=r-1: ANS=min(ANS,abs(getvalues2(l-1,yy-1)-x)) for i in [l-1,r-1,xx-1,xx,yy-1,yy]: if l-1<=i<=r-1: ANS=min(ANS,abs(A[i]-x)) print(ANS)