結果

問題 No.1332 Range Nearest Query
ユーザー mkawa2mkawa2
提出日時 2021-01-09 16:10:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,101 ms / 2,500 ms
コード長 3,383 bytes
コンパイル時間 390 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 151,224 KB
最終ジャッジ日時 2024-11-17 21:11:54
合計ジャッジ時間 61,710 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,696 KB
testcase_01 AC 36 ms
53,172 KB
testcase_02 AC 35 ms
53,448 KB
testcase_03 AC 1,917 ms
119,456 KB
testcase_04 AC 1,884 ms
119,216 KB
testcase_05 AC 1,870 ms
119,724 KB
testcase_06 AC 970 ms
146,472 KB
testcase_07 AC 987 ms
146,432 KB
testcase_08 AC 982 ms
146,496 KB
testcase_09 AC 997 ms
146,632 KB
testcase_10 AC 984 ms
146,928 KB
testcase_11 AC 996 ms
146,516 KB
testcase_12 AC 1,006 ms
146,384 KB
testcase_13 AC 1,004 ms
146,696 KB
testcase_14 AC 981 ms
146,388 KB
testcase_15 AC 1,014 ms
146,468 KB
testcase_16 AC 2,064 ms
146,360 KB
testcase_17 AC 2,098 ms
146,436 KB
testcase_18 AC 2,088 ms
146,256 KB
testcase_19 AC 2,009 ms
146,364 KB
testcase_20 AC 2,097 ms
146,476 KB
testcase_21 AC 2,093 ms
146,452 KB
testcase_22 AC 2,101 ms
146,504 KB
testcase_23 AC 2,101 ms
146,328 KB
testcase_24 AC 2,039 ms
146,720 KB
testcase_25 AC 2,101 ms
146,588 KB
testcase_26 AC 472 ms
143,356 KB
testcase_27 AC 448 ms
143,040 KB
testcase_28 AC 524 ms
98,168 KB
testcase_29 AC 593 ms
98,980 KB
testcase_30 AC 631 ms
99,248 KB
testcase_31 AC 336 ms
94,512 KB
testcase_32 AC 606 ms
98,596 KB
testcase_33 AC 637 ms
98,560 KB
testcase_34 AC 452 ms
97,848 KB
testcase_35 AC 566 ms
98,484 KB
testcase_36 AC 543 ms
97,204 KB
testcase_37 AC 583 ms
98,736 KB
testcase_38 AC 1,460 ms
113,728 KB
testcase_39 AC 834 ms
99,212 KB
testcase_40 AC 2,045 ms
151,224 KB
testcase_41 AC 1,029 ms
103,724 KB
testcase_42 AC 1,430 ms
113,984 KB
testcase_43 AC 1,190 ms
105,964 KB
testcase_44 AC 1,762 ms
116,476 KB
testcase_45 AC 1,634 ms
116,844 KB
testcase_46 AC 1,332 ms
109,384 KB
testcase_47 AC 1,550 ms
116,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def II(): return int(sys.stdin.buffer.readline())
def MI(): return map(int, sys.stdin.buffer.readline().split())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
inf = 10**16
# md = 998244353
md = 10**9+7

class SegtreeMin():
    def __init__(self, n):
        self.inf = 10**16
        aa = []
        if not isinstance(n, int):
            aa = n
            n = len(aa)
        self.tree_width = tw = 1 << (n-1).bit_length()
        self.tree = [self.inf]*(tw*2-1)
        if aa:
            self.tree[tw-1:tw-1+len(aa)] = aa
            for i in range(tw-2, -1, -1):
                self.tree[i] = min(self.tree[i*2+1], self.tree[i*2+2])

    def update(self, i, a):
        seg_i = self.tree_width-1+i
        self.tree[seg_i] = a
        while seg_i != 0:
            seg_i = (seg_i-1)//2
            self.tree[seg_i] = min(self.tree[seg_i*2+1], self.tree[seg_i*2+2])

    def element(self, i):
        return self.tree[self.tree_width-1+i]

    # [l,r)の最小値
    def min(self, l, r):
        res = self.inf
        stack = [(0, 0, self.tree_width)]
        while stack:
            seg_i, segL, segR = stack.pop()
            if l <= segL and segR <= r:
                if self.tree[seg_i] < res: res = self.tree[seg_i]
            else:
                segM = (segL+segR)//2
                if r > segL and segM > l:
                    stack.append((seg_i*2+1, segL, segM))
                if r > segM and segR > l:
                    stack.append((seg_i*2+2, segM, segR))
        return res

class SegtreeMax():
    def __init__(self, n):
        self.inf = 10**16
        aa = []
        if not isinstance(n, int):
            aa = n
            n = len(aa)
        self.tree_width = tw = 1 << (n-1).bit_length()
        self.tree = [-self.inf]*(tw*2-1)
        if aa:
            self.tree[tw-1:tw-1+len(aa)] = aa
            for i in range(tw-2, -1, -1):
                self.tree[i] = max(self.tree[i*2+1], self.tree[i*2+2])

    def update(self, i, a):
        seg_i = self.tree_width-1+i
        self.tree[seg_i] = a
        while seg_i != 0:
            seg_i = (seg_i-1)//2
            self.tree[seg_i] = max(self.tree[seg_i*2+1], self.tree[seg_i*2+2])

    def element(self, i):
        return self.tree[self.tree_width-1+i]

    # [l,r)の最大値
    def max(self, l, r):
        res = -self.inf
        stack = [(0, 0, self.tree_width)]
        while stack:
            seg_i, segL, segR = stack.pop()
            if l <= segL and segR <= r:
                if self.tree[seg_i] > res: res = self.tree[seg_i]
            else:
                segM = (segL+segR)//2
                if r > segL and segM > l:
                    stack.append((seg_i*2+1, segL, segM))
                if r > segM and segR > l:
                    stack.append((seg_i*2+2, segM, segR))
        return res

n=II()
xx=LI()
q=II()
xlri=[]
for i in range(q):
    l,r,x=MI()
    l-=1
    xlri.append((x,l,r,i))
xlri.sort()

xj=[(x,j) for j,x in enumerate(xx)]
xj.sort()

ans=[-1]*q
mx=SegtreeMax(n)
p=0
for x,l,r,i in xlri:
    while p<n and xj[p][0]<=x:
        x0,j=xj[p]
        mx.update(j,x0)
        p+=1
    ans[i]=x-mx.max(l,r)

mn=SegtreeMin(n)
p=n-1
for x,l,r,i in xlri[::-1]:
    while p>=0 and xj[p][0]>=x:
        x0,j=xj[p]
        mn.update(j,x0)
        p-=1
    cur=mn.min(l,r)-x
    if cur<ans[i]:ans[i]=cur

print(*ans,sep="\n")
0