結果

問題 No.1332 Range Nearest Query
ユーザー titiatitia
提出日時 2021-01-14 05:22:46
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 3,184 bytes
コンパイル時間 1,880 ms
コンパイル使用メモリ 84,952 KB
実行使用メモリ 130,856 KB
最終ジャッジ日時 2023-08-15 10:13:24
合計ジャッジ時間 25,159 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
70,620 KB
testcase_01 AC 71 ms
70,680 KB
testcase_02 AC 72 ms
70,664 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 353 ms
130,052 KB
testcase_07 AC 352 ms
130,444 KB
testcase_08 AC 349 ms
130,176 KB
testcase_09 AC 367 ms
130,680 KB
testcase_10 AC 351 ms
130,656 KB
testcase_11 AC 357 ms
130,568 KB
testcase_12 AC 355 ms
130,444 KB
testcase_13 AC 357 ms
130,304 KB
testcase_14 AC 357 ms
130,264 KB
testcase_15 AC 353 ms
130,604 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 270 ms
130,144 KB
testcase_27 AC 289 ms
130,256 KB
testcase_28 AC 224 ms
88,804 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 199 ms
88,412 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 210 ms
88,832 KB
testcase_35 AC 218 ms
89,200 KB
testcase_36 AC 216 ms
88,612 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 -
権限があれば一括ダウンロードができます

ソースコード

diff #

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=0

    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)

    ANS=1<<30
    for i in [l-1,r-1,xx-1,xx,xx+1,yy-1,yy,yy+1]:
        if l-1<=i<=r-1:
            ANS=min(ANS,abs(A[i]-x))

    print(ANS)


0