結果

問題 No.1332 Range Nearest Query
ユーザー convexineqconvexineq
提出日時 2021-01-09 15:52:02
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,000 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 82,556 KB
実行使用メモリ 218,176 KB
最終ジャッジ日時 2024-04-28 23:05:33
合計ジャッジ時間 79,391 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,736 KB
testcase_01 AC 40 ms
52,736 KB
testcase_02 AC 39 ms
53,120 KB
testcase_03 AC 2,410 ms
188,176 KB
testcase_04 AC 2,364 ms
187,540 KB
testcase_05 AC 2,475 ms
188,052 KB
testcase_06 AC 1,317 ms
216,296 KB
testcase_07 AC 1,353 ms
216,352 KB
testcase_08 AC 1,305 ms
216,508 KB
testcase_09 AC 1,303 ms
216,532 KB
testcase_10 AC 1,310 ms
216,016 KB
testcase_11 AC 1,307 ms
216,224 KB
testcase_12 AC 1,346 ms
216,824 KB
testcase_13 AC 1,307 ms
216,228 KB
testcase_14 AC 1,349 ms
216,028 KB
testcase_15 AC 1,339 ms
217,504 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 788 ms
216,216 KB
testcase_27 AC 755 ms
216,348 KB
testcase_28 AC 369 ms
76,928 KB
testcase_29 AC 404 ms
77,700 KB
testcase_30 AC 454 ms
77,520 KB
testcase_31 AC 313 ms
77,420 KB
testcase_32 AC 497 ms
78,092 KB
testcase_33 AC 474 ms
78,416 KB
testcase_34 AC 341 ms
76,788 KB
testcase_35 AC 369 ms
77,356 KB
testcase_36 AC 371 ms
77,288 KB
testcase_37 AC 387 ms
77,056 KB
testcase_38 AC 2,161 ms
137,028 KB
testcase_39 AC 1,247 ms
83,688 KB
testcase_40 TLE -
testcase_41 AC 1,681 ms
98,060 KB
testcase_42 AC 2,017 ms
134,148 KB
testcase_43 AC 1,837 ms
110,836 KB
testcase_44 AC 2,268 ms
179,388 KB
testcase_45 AC 2,214 ms
164,756 KB
testcase_46 AC 1,999 ms
122,440 KB
testcase_47 AC 2,249 ms
158,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class segment_tree:
    __slots__ = ["op_M", "e_M","N","N0","dat"]
    def __init__(self, N, operator_M, e_M):
        self.op_M = operator_M
        self.e_M = e_M
        self.N = N
        self.N0 = 1<<(N-1).bit_length()
        self.dat = [self.e_M]*(2*self.N0)
    
    # 長さNの配列 initial で初期化
    def build(self, initial):
        assert self.N == len(initial)
        self.dat[self.N0:self.N0+len(initial)] = initial[:]
        for k in range(self.N0-1,0,-1):
            self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1])

    # a_k の値を x に更新
    def update(self,k,x):
        k += self.N0
        self.dat[k] = x
        k >>= 1
        while k:
            self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1])
            k >>= 1

    # 区間[L,R]をopでまとめる
    def query(self,L,R):
        L += self.N0; R += self.N0 + 1 
        sl = sr = self.e_M
        while L < R:
            if R & 1:
                R -= 1
                sr = self.op_M(self.dat[R],sr)
            if L & 1:
                sl = self.op_M(sl,self.dat[L])
                L += 1
            L >>= 1; R >>= 1
        return self.op_M(sl,sr)

    def get(self, k): #k番目の値を取得。query[k,k]と同じ
        return self.dat[k+self.N0]


n = int(input())
*a, = map(int,input().split())

def merge(a,b):
    return sorted(a+b)
seg = segment_tree(n, merge, [])
seg.build([[ai] for ai in a])

def getans(lst,x):
    v = INF
    idx = bisect_left(lst,x)
    if idx < len(lst):
        v = lst[idx]-x
    if idx:
        v = min(v,x-lst[idx-1])
    return v

INF = 10**9
from bisect import bisect_left, bisect_right
Q = int(input())
N0 = seg.N0
for _ in range(Q):
    L,R,x = map(int,input().split())
    L += N0-1; R += N0
    ans = INF
    while L < R:
        if R & 1:
            R -= 1
            ans = min(ans,getans(seg.dat[R],x))
        if L & 1:
            ans = min(ans,getans(seg.dat[L],x))
            L += 1
        L >>= 1; R >>= 1
    print(ans)
0