結果

問題 No.1332 Range Nearest Query
ユーザー mkawa2mkawa2
提出日時 2021-01-09 16:10:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,398 ms / 2,500 ms
コード長 3,383 bytes
コンパイル時間 693 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 147,244 KB
最終ジャッジ日時 2024-12-16 15:02:25
合計ジャッジ時間 70,528 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,352 KB
testcase_01 AC 41 ms
52,736 KB
testcase_02 AC 40 ms
52,992 KB
testcase_03 AC 2,163 ms
119,720 KB
testcase_04 AC 2,098 ms
119,280 KB
testcase_05 AC 2,148 ms
119,656 KB
testcase_06 AC 1,102 ms
146,396 KB
testcase_07 AC 1,089 ms
146,264 KB
testcase_08 AC 1,098 ms
146,636 KB
testcase_09 AC 1,099 ms
146,768 KB
testcase_10 AC 1,092 ms
146,520 KB
testcase_11 AC 1,072 ms
146,400 KB
testcase_12 AC 1,091 ms
146,532 KB
testcase_13 AC 1,108 ms
146,400 KB
testcase_14 AC 1,104 ms
146,532 KB
testcase_15 AC 1,132 ms
146,484 KB
testcase_16 AC 2,285 ms
146,504 KB
testcase_17 AC 2,310 ms
147,120 KB
testcase_18 AC 2,321 ms
146,796 KB
testcase_19 AC 2,379 ms
147,244 KB
testcase_20 AC 2,342 ms
146,612 KB
testcase_21 AC 2,351 ms
146,692 KB
testcase_22 AC 2,391 ms
146,608 KB
testcase_23 AC 2,398 ms
146,944 KB
testcase_24 AC 2,367 ms
146,524 KB
testcase_25 AC 2,397 ms
146,636 KB
testcase_26 AC 505 ms
143,404 KB
testcase_27 AC 499 ms
142,940 KB
testcase_28 AC 592 ms
98,360 KB
testcase_29 AC 660 ms
99,244 KB
testcase_30 AC 730 ms
99,516 KB
testcase_31 AC 381 ms
94,640 KB
testcase_32 AC 708 ms
98,696 KB
testcase_33 AC 724 ms
98,820 KB
testcase_34 AC 506 ms
97,840 KB
testcase_35 AC 619 ms
98,864 KB
testcase_36 AC 616 ms
97,328 KB
testcase_37 AC 663 ms
98,744 KB
testcase_38 AC 1,627 ms
113,984 KB
testcase_39 AC 919 ms
99,212 KB
testcase_40 AC 2,322 ms
144,016 KB
testcase_41 AC 1,171 ms
103,960 KB
testcase_42 AC 1,574 ms
114,044 KB
testcase_43 AC 1,332 ms
106,424 KB
testcase_44 AC 1,998 ms
116,924 KB
testcase_45 AC 1,808 ms
116,952 KB
testcase_46 AC 1,547 ms
109,368 KB
testcase_47 AC 1,769 ms
116,152 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