結果
問題 | No.1332 Range Nearest Query |
ユーザー | titia |
提出日時 | 2022-08-04 01:39:40 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,779 ms / 2,500 ms |
コード長 | 2,322 bytes |
コンパイル時間 | 342 ms |
コンパイル使用メモリ | 86,724 KB |
実行使用メモリ | 191,532 KB |
最終ジャッジ日時 | 2023-10-11 21:24:28 |
合計ジャッジ時間 | 59,373 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 78 ms
71,152 KB |
testcase_01 | AC | 76 ms
71,308 KB |
testcase_02 | AC | 77 ms
71,384 KB |
testcase_03 | AC | 1,584 ms
168,108 KB |
testcase_04 | AC | 1,583 ms
171,148 KB |
testcase_05 | AC | 1,595 ms
170,268 KB |
testcase_06 | AC | 974 ms
184,920 KB |
testcase_07 | AC | 975 ms
182,144 KB |
testcase_08 | AC | 942 ms
175,644 KB |
testcase_09 | AC | 973 ms
186,484 KB |
testcase_10 | AC | 962 ms
186,824 KB |
testcase_11 | AC | 972 ms
181,660 KB |
testcase_12 | AC | 979 ms
183,688 KB |
testcase_13 | AC | 971 ms
181,312 KB |
testcase_14 | AC | 950 ms
181,512 KB |
testcase_15 | AC | 980 ms
184,972 KB |
testcase_16 | AC | 1,719 ms
188,556 KB |
testcase_17 | AC | 1,704 ms
176,152 KB |
testcase_18 | AC | 1,729 ms
189,316 KB |
testcase_19 | AC | 1,696 ms
176,680 KB |
testcase_20 | AC | 1,742 ms
191,292 KB |
testcase_21 | AC | 1,737 ms
190,532 KB |
testcase_22 | AC | 1,721 ms
191,532 KB |
testcase_23 | AC | 1,715 ms
191,204 KB |
testcase_24 | AC | 1,740 ms
190,192 KB |
testcase_25 | AC | 1,779 ms
188,948 KB |
testcase_26 | AC | 578 ms
168,612 KB |
testcase_27 | AC | 570 ms
168,944 KB |
testcase_28 | AC | 470 ms
111,492 KB |
testcase_29 | AC | 527 ms
111,300 KB |
testcase_30 | AC | 567 ms
111,624 KB |
testcase_31 | AC | 326 ms
106,796 KB |
testcase_32 | AC | 578 ms
111,448 KB |
testcase_33 | AC | 579 ms
111,564 KB |
testcase_34 | AC | 419 ms
111,476 KB |
testcase_35 | AC | 496 ms
111,536 KB |
testcase_36 | AC | 493 ms
111,492 KB |
testcase_37 | AC | 525 ms
111,236 KB |
testcase_38 | AC | 1,182 ms
137,496 KB |
testcase_39 | AC | 670 ms
110,064 KB |
testcase_40 | AC | 1,718 ms
182,112 KB |
testcase_41 | AC | 818 ms
115,200 KB |
testcase_42 | AC | 1,152 ms
138,152 KB |
testcase_43 | AC | 1,002 ms
127,960 KB |
testcase_44 | AC | 1,399 ms
150,652 KB |
testcase_45 | AC | 1,296 ms
142,268 KB |
testcase_46 | AC | 1,106 ms
138,600 KB |
testcase_47 | AC | 1,229 ms
142,656 KB |
ソースコード
import sys input = sys.stdin.readline from operator import itemgetter 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_function(x,y): # Segment treeで扱うfunction return max(x,y) seg_el=1<<(N.bit_length()) # Segment treeの台の要素数 SEG=[-1<<60]*(2*seg_el) # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化 def update(n,x,seg_el): # A[n]をxへ更新 i=n+seg_el SEG[i]=x i>>=1 # 子ノードへ while i!=0: SEG[i]=seg_function(SEG[i*2],SEG[i*2+1]) i>>=1 def getvalues(l,r): # 区間[l,r)に関するseg_functionを調べる L=l+seg_el R=r+seg_el ANS=-1<<60 while L<R: if L & 1: ANS=seg_function(ANS , SEG[L]) L+=1 if R & 1: R-=1 ANS=seg_function(ANS , SEG[R]) L>>=1 R>>=1 return ANS ANS=[1<<60]*Q QU=[] for i in range(N): QU.append((A[i],i)) for i in range(Q): l,r,x=Query[i] QU.append((x,1<<60,l,r,i)) QU.sort(key=itemgetter(0)) for qu in QU: if len(qu)==2: ai,i=qu update(i,ai,seg_el) else: x,_,l,r,ind=qu vv=getvalues(l-1,r) ANS[ind]=min(ANS[ind],abs(vv-x)) seg_el=1<<(N.bit_length()) # Segment treeの台の要素数 SEG=[1<<60]*(2*seg_el) # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化 def seg_function(x,y): # Segment treeで扱うfunction return min(x,y) def update(n,x,seg_el): # A[n]をxへ更新 i=n+seg_el SEG[i]=x i>>=1 # 子ノードへ while i!=0: SEG[i]=seg_function(SEG[i*2],SEG[i*2+1]) i>>=1 def getvalues(l,r): # 区間[l,r)に関するseg_functionを調べる L=l+seg_el R=r+seg_el ANS=1<<60 while L<R: if L & 1: ANS=seg_function(ANS , SEG[L]) L+=1 if R & 1: R-=1 ANS=seg_function(ANS , SEG[R]) L>>=1 R>>=1 return ANS QU.sort(key=itemgetter(0),reverse=True) for qu in QU: if len(qu)==2: ai,i=qu update(i,ai,seg_el) else: x,_,l,r,ind=qu vv=getvalues(l-1,r) ANS[ind]=min(ANS[ind],abs(vv-x)) print("\n".join(map(str,ANS)))