結果

問題 No.1332 Range Nearest Query
ユーザー mkawa2mkawa2
提出日時 2021-01-09 14:25:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,342 ms / 2,500 ms
コード長 5,800 bytes
コンパイル時間 626 ms
コンパイル使用メモリ 82,364 KB
実行使用メモリ 155,528 KB
最終ジャッジ日時 2024-11-17 18:06:07
合計ジャッジ時間 61,574 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,120 KB
testcase_01 AC 37 ms
52,864 KB
testcase_02 AC 37 ms
52,992 KB
testcase_03 AC 1,874 ms
139,080 KB
testcase_04 AC 1,788 ms
138,992 KB
testcase_05 AC 1,852 ms
139,036 KB
testcase_06 AC 927 ms
155,096 KB
testcase_07 AC 902 ms
155,432 KB
testcase_08 AC 868 ms
155,300 KB
testcase_09 AC 971 ms
155,388 KB
testcase_10 AC 972 ms
155,444 KB
testcase_11 AC 918 ms
155,256 KB
testcase_12 AC 916 ms
155,184 KB
testcase_13 AC 980 ms
155,204 KB
testcase_14 AC 1,014 ms
155,196 KB
testcase_15 AC 959 ms
155,252 KB
testcase_16 AC 2,151 ms
155,528 KB
testcase_17 AC 1,941 ms
155,408 KB
testcase_18 AC 1,974 ms
155,452 KB
testcase_19 AC 2,238 ms
155,336 KB
testcase_20 AC 2,111 ms
155,136 KB
testcase_21 AC 2,130 ms
155,228 KB
testcase_22 AC 2,073 ms
155,384 KB
testcase_23 AC 2,096 ms
155,468 KB
testcase_24 AC 2,145 ms
155,260 KB
testcase_25 AC 2,236 ms
155,508 KB
testcase_26 AC 564 ms
151,868 KB
testcase_27 AC 383 ms
153,524 KB
testcase_28 AC 512 ms
97,820 KB
testcase_29 AC 523 ms
97,920 KB
testcase_30 AC 622 ms
98,396 KB
testcase_31 AC 314 ms
93,952 KB
testcase_32 AC 613 ms
97,960 KB
testcase_33 AC 629 ms
98,296 KB
testcase_34 AC 433 ms
96,136 KB
testcase_35 AC 560 ms
98,200 KB
testcase_36 AC 546 ms
97,220 KB
testcase_37 AC 586 ms
98,300 KB
testcase_38 AC 1,410 ms
114,920 KB
testcase_39 AC 759 ms
99,072 KB
testcase_40 AC 2,342 ms
151,892 KB
testcase_41 AC 985 ms
101,460 KB
testcase_42 AC 1,336 ms
114,472 KB
testcase_43 AC 1,176 ms
107,008 KB
testcase_44 AC 1,906 ms
122,320 KB
testcase_45 AC 1,792 ms
120,396 KB
testcase_46 AC 1,418 ms
110,216 KB
testcase_47 AC 1,482 ms
117,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**6)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def MI(): return map(int, sys.stdin.buffer.readline().split())
def MI1(): return map(int1, sys.stdin.buffer.readline().split())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LI1(): return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)]]
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

    # [l,r)でaより小さい要素のうち最右のインデックス
    def LessR(self, l, r, a):
        stack = [(0, 0, self.tree_width)]
        while stack:
            u, ul, ur = stack.pop()
            if ur-ul == 1: return u-self.tree_width+1
            um = (ul+ur)//2
            if self.tree[u*2+1] < a and ul < r and l < um:
                stack.append((u*2+1, ul, um))
            if self.tree[u*2+2] < a and um < r and l < ur:
                stack.append((u*2+2, um, ur))
        return -1

    # [l,r)でaより小さい要素のうち最左のインデックス
    def LessL(self, l, r, a):
        stack = [(0, 0, self.tree_width)]
        while stack:
            u, ul, ur = stack.pop()
            if ur-ul == 1: return u-self.tree_width+1
            um = (ul+ur)//2
            if self.tree[u*2+2] < a and um < r and l < ur:
                stack.append((u*2+2, um, ur))
            if self.tree[u*2+1] < a and ul < r and l < um:
                stack.append((u*2+1, ul, um))
        return -1

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

    # [l,r)でaより大きい要素のうち最右のインデックス
    def OverR(self, l, r, a):
        stack = [(0, 0, self.tree_width)]
        while stack:
            u, ul, ur = stack.pop()
            if ur-ul == 1: return u-self.tree_width+1
            um = (ul+ur)//2
            if self.tree[u*2+1] > a and ul < r and l < um:
                stack.append((u*2+1, ul, um))
            if self.tree[u*2+2] > a and um < r and l < ur:
                stack.append((u*2+2, um, ur))
        return -1

    # [l,r)でaより大きい要素のうち最左のインデックス
    def OverL(self, l, r, a):
        stack = [(0, 0, self.tree_width)]
        while stack:
            u, ul, ur = stack.pop()
            if ur-ul == 1: return u-self.tree_width+1
            um = (ul+ur)//2
            if self.tree[u*2+2] > a and um < r and l < ur:
                stack.append((u*2+2, um, ur))
            if self.tree[u*2+1] > a and ul < r and l < um:
                stack.append((u*2+1, ul, um))
        return -1

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()

mn=SegtreeMin(xx)
mx=SegtreeMax(n)
ans=[-1]*q
xj=[(x,j) for j,x in enumerate(xx)]
xj.sort(reverse=True)

for x,l,r,i in xlri:
    while xj and xj[-1][0]<x:
        x0,j=xj.pop()
        mn.update(j,inf)
        mx.update(j,x0)
    ans[i]=min(mn.min(l,r)-x,x-mx.max(l,r))

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