結果

問題 No.1332 Range Nearest Query
ユーザー titiatitia
提出日時 2021-01-14 05:34:33
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,342 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 87,304 KB
実行使用メモリ 131,632 KB
最終ジャッジ日時 2023-08-15 10:27:37
合計ジャッジ時間 22,335 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,172 KB
testcase_01 WA -
testcase_02 AC 69 ms
71,044 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 339 ms
130,872 KB
testcase_07 AC 338 ms
130,932 KB
testcase_08 AC 346 ms
130,876 KB
testcase_09 AC 347 ms
130,936 KB
testcase_10 AC 348 ms
131,060 KB
testcase_11 AC 341 ms
131,084 KB
testcase_12 AC 348 ms
130,852 KB
testcase_13 AC 344 ms
130,828 KB
testcase_14 AC 349 ms
131,032 KB
testcase_15 AC 364 ms
130,872 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 272 ms
131,180 KB
testcase_27 AC 310 ms
131,108 KB
testcase_28 AC 251 ms
90,628 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 218 ms
89,824 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 257 ms
90,272 KB
testcase_35 AC 262 ms
90,204 KB
testcase_36 AC 265 ms
91,076 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=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)-x))
    if l-1<=yy-1<=r-1:
        ANS=min(ANS,abs(getvalues2(l-1,yy)-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)


0