結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2020-12-31 06:35:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,565 ms / 2,500 ms
コード長 3,967 bytes
コンパイル時間 512 ms
コンパイル使用メモリ 82,140 KB
実行使用メモリ 140,032 KB
最終ジャッジ日時 2024-10-08 22:23:36
合計ジャッジ時間 47,344 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
67,840 KB
testcase_01 AC 77 ms
67,840 KB
testcase_02 AC 76 ms
67,840 KB
testcase_03 AC 1,470 ms
131,328 KB
testcase_04 AC 1,459 ms
131,456 KB
testcase_05 AC 1,453 ms
131,712 KB
testcase_06 AC 791 ms
140,032 KB
testcase_07 AC 807 ms
139,904 KB
testcase_08 AC 820 ms
139,520 KB
testcase_09 AC 789 ms
139,520 KB
testcase_10 AC 845 ms
139,904 KB
testcase_11 AC 777 ms
139,648 KB
testcase_12 AC 741 ms
139,776 KB
testcase_13 AC 761 ms
139,776 KB
testcase_14 AC 767 ms
139,520 KB
testcase_15 AC 886 ms
139,392 KB
testcase_16 AC 1,481 ms
139,904 KB
testcase_17 AC 1,473 ms
139,648 KB
testcase_18 AC 1,512 ms
139,776 KB
testcase_19 AC 1,509 ms
139,776 KB
testcase_20 AC 1,532 ms
140,032 KB
testcase_21 AC 1,565 ms
139,392 KB
testcase_22 AC 1,506 ms
139,520 KB
testcase_23 AC 1,495 ms
139,648 KB
testcase_24 AC 1,563 ms
139,648 KB
testcase_25 AC 1,513 ms
139,648 KB
testcase_26 AC 501 ms
139,392 KB
testcase_27 AC 544 ms
139,648 KB
testcase_28 AC 325 ms
84,224 KB
testcase_29 AC 435 ms
84,672 KB
testcase_30 AC 412 ms
84,756 KB
testcase_31 AC 166 ms
83,964 KB
testcase_32 AC 385 ms
84,832 KB
testcase_33 AC 401 ms
84,440 KB
testcase_34 AC 257 ms
83,712 KB
testcase_35 AC 315 ms
84,096 KB
testcase_36 AC 308 ms
84,096 KB
testcase_37 AC 409 ms
84,932 KB
testcase_38 AC 1,189 ms
116,632 KB
testcase_39 AC 524 ms
87,664 KB
testcase_40 AC 1,512 ms
135,680 KB
testcase_41 AC 707 ms
95,020 KB
testcase_42 AC 1,163 ms
114,692 KB
testcase_43 AC 796 ms
102,612 KB
testcase_44 AC 1,193 ms
125,952 KB
testcase_45 AC 1,250 ms
126,832 KB
testcase_46 AC 1,044 ms
109,712 KB
testcase_47 AC 1,086 ms
121,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Refernce: https://github.com/Mitarushi/ACL-Python
from typing import Callable, TypeVar, List

T = TypeVar('T')


class SegTree:
    def __init__(self, v: List[T], op: Callable[[T, T], T], e: Callable[[], T]) -> None:
        self._n = len(v)
        self.log = (self._n - 1).bit_length()
        self.size = 1 << self.log
        self.d = [e() for _ in range(2 * self.size)]
        self.op = op
        self.e = e
        for i in range(self._n):
            self.d[self.size + i] = v[i]
        for i in reversed(range(1, self.size)):
            self.__update__(i)

    @classmethod
    def init_e(cls, n: int, op: Callable[[T, T], T], e: Callable[[], T]) -> 'SegTree':
        return cls([e() for _ in range(n)], op, e)

    def set(self, p: int, x: T) -> None:
        assert 0 <= p < self._n
        p += self.size
        self.d[p] = x
        for i in range(1, self.log + 1):
            self.__update__(p >> i)

    def get(self, p: int) -> T:
        assert 0 <= p < self._n
        return self.d[p + self.size]

    def prod(self, l: int, r: int) -> T:
        assert 0 <= l <= self._n and 0 <= r <= self._n
        sml = self.e()
        smr = self.e()
        l += self.size
        r += self.size

        while l < r:
            if l & 1:
                sml = self.op(sml, self.d[l])
                l += 1
            if r & 1:
                r -= 1
                smr = self.op(self.d[r], smr)
            l >>= 1
            r >>= 1
        return self.op(sml, smr)

    def all_prod(self) -> T:
        return self.d[1]

    def max_right(self, l: int, f: Callable[[T], bool]):
        assert 0 <= l <= self._n
        assert f(self.e())
        if l == self._n:
            return self._n
        l += self.size
        sm = self.e()
        while True:
            while l % 2 == 0:
                l >>= 1
            if not f(self.op(sm, self.d[l])):
                while l < self.size:
                    l *= 2
                    if f(self.op(sm, self.d[l])):
                        sm = self.op(sm, self.d[l])
                        l += 1
                return l - self.size
            sm = self.op(sm, self.d[l])
            l += 1
            if (l & -l) == l:
                return self._n

    def min_left(self, r: int, f: Callable[[T], bool]):
        assert 0 <= r <= self._n
        assert f(self.e())
        if r == 0:
            return 0
        r += self.size
        sm = self.e()
        while True:
            r -= 1
            while r > 1 and r % 2:
                r >>= 1
            if not f(self.op(self.d[r], sm)):
                while r < self.size:
                    r = 2 * r + 1
                    if f(self.op(self.d[r], sm)):
                        sm = self.op(self.d[r], sm)
                        r -= 1
                return r + 1 - self.size
            sm = self.op(self.d[r], sm)
            if (r & -r) == r:
                return 0

    def __update__(self, k: int) -> None:
        self.d[k] = self.op(self.d[2 * k], self.d[2 * k + 1])

import sys
input=sys.stdin.readline
import bisect

inf=int(1e9)
N=int(input())
A=[-inf]*N
X=list(map(int,input().split()))
Q=int(input())
ind=[[]for i in range(N)]
lef=[0]*Q
po=[0]*Q
for i in range(Q):
    l,r,x=map(int, input().split())
    ind[r-1].append(i)
    lef[i]=l-1
    po[i]=x
ans=[inf]*Q
seg=SegTree(A,max,lambda:-inf)
Y=sorted(X)
for i in range(N):
    p=bisect.bisect_left(Y,X[i])
    seg.set(p,i)
    for j in range(len(ind[i])):
        index=ind[i][j]
        left=lef[index]
        point=po[index]
        right=i
        now=bisect.bisect_left(Y,point)
        index2=seg.max_right(now,lambda  z: z<left)
        if index2<N and index2>=0 and ans[index]>Y[index2]-point:
            ans[index]=Y[index2]-point
        index2=seg.min_left(now,lambda  z: z<left)
        index2-=1
        if index2<N and index2>=0 and ans[index]>point-Y[index2]:
            ans[index]=point-Y[index2]
for i in range(Q):
    print(ans[i])
0