結果

問題 No.1332 Range Nearest Query
ユーザー mkawa2mkawa2
提出日時 2021-01-09 17:11:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,071 ms / 2,500 ms
コード長 4,096 bytes
コンパイル時間 688 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 153,920 KB
最終ジャッジ日時 2024-11-17 23:01:45
合計ジャッジ時間 60,533 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
67,328 KB
testcase_01 AC 79 ms
68,096 KB
testcase_02 AC 68 ms
67,712 KB
testcase_03 AC 2,039 ms
140,240 KB
testcase_04 AC 1,796 ms
138,420 KB
testcase_05 AC 1,764 ms
138,692 KB
testcase_06 AC 949 ms
153,352 KB
testcase_07 AC 1,073 ms
153,188 KB
testcase_08 AC 918 ms
153,104 KB
testcase_09 AC 923 ms
153,704 KB
testcase_10 AC 905 ms
153,616 KB
testcase_11 AC 917 ms
153,408 KB
testcase_12 AC 932 ms
153,220 KB
testcase_13 AC 924 ms
153,240 KB
testcase_14 AC 909 ms
153,108 KB
testcase_15 AC 1,050 ms
153,228 KB
testcase_16 AC 1,994 ms
153,344 KB
testcase_17 AC 2,069 ms
153,164 KB
testcase_18 AC 2,043 ms
153,156 KB
testcase_19 AC 2,071 ms
153,920 KB
testcase_20 AC 2,028 ms
153,900 KB
testcase_21 AC 2,015 ms
153,724 KB
testcase_22 AC 1,972 ms
153,668 KB
testcase_23 AC 2,007 ms
153,284 KB
testcase_24 AC 1,992 ms
153,280 KB
testcase_25 AC 1,981 ms
153,420 KB
testcase_26 AC 474 ms
148,972 KB
testcase_27 AC 467 ms
150,792 KB
testcase_28 AC 527 ms
99,860 KB
testcase_29 AC 575 ms
100,112 KB
testcase_30 AC 601 ms
100,628 KB
testcase_31 AC 374 ms
97,200 KB
testcase_32 AC 625 ms
99,824 KB
testcase_33 AC 602 ms
100,584 KB
testcase_34 AC 461 ms
98,576 KB
testcase_35 AC 537 ms
100,600 KB
testcase_36 AC 526 ms
98,680 KB
testcase_37 AC 605 ms
100,108 KB
testcase_38 AC 1,350 ms
127,236 KB
testcase_39 AC 766 ms
102,028 KB
testcase_40 AC 1,985 ms
149,204 KB
testcase_41 AC 930 ms
106,056 KB
testcase_42 AC 1,319 ms
125,164 KB
testcase_43 AC 1,101 ms
113,940 KB
testcase_44 AC 1,617 ms
135,376 KB
testcase_45 AC 1,515 ms
132,352 KB
testcase_46 AC 1,254 ms
120,348 KB
testcase_47 AC 1,404 ms
130,368 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

import typing

class SegTree:
    def __init__(self,
                 op: typing.Callable[[typing.Any, typing.Any], typing.Any],
                 e: typing.Any,
                 v: typing.Union[int, typing.List[typing.Any]]) -> None:
        self._op = op
        self._e = e

        if isinstance(v, int):
            v = [e] * v

        self._n = len(v)
        self._log = (self._n-1).bit_length()
        self._size = 1 << self._log
        self._d = [e] * (2 * self._size)

        for i in range(self._n):
            self._d[self._size + i] = v[i]
        for i in range(self._size - 1, 0, -1):
            self._update(i)

    def set(self, p: int, x: typing.Any) -> 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) -> typing.Any:
        assert 0 <= p < self._n

        return self._d[p + self._size]

    def prod(self, left: int, right: int) -> typing.Any:
        assert 0 <= left <= right <= self._n
        sml = self._e
        smr = self._e
        left += self._size
        right += self._size

        while left < right:
            if left & 1:
                sml = self._op(sml, self._d[left])
                left += 1
            if right & 1:
                right -= 1
                smr = self._op(self._d[right], smr)
            left >>= 1
            right >>= 1

        return self._op(sml, smr)

    def all_prod(self) -> typing.Any:
        return self._d[1]

    def max_right(self, left: int,
                  f: typing.Callable[[typing.Any], bool]) -> int:
        assert 0 <= left <= self._n
        assert f(self._e)

        if left == self._n:
            return self._n

        left += self._size
        sm = self._e

        first = True
        while first or (left & -left) != left:
            first = False
            while left % 2 == 0:
                left >>= 1
            if not f(self._op(sm, self._d[left])):
                while left < self._size:
                    left *= 2
                    if f(self._op(sm, self._d[left])):
                        sm = self._op(sm, self._d[left])
                        left += 1
                return left - self._size
            sm = self._op(sm, self._d[left])
            left += 1

        return self._n

    def min_left(self, right: int,
                 f: typing.Callable[[typing.Any], bool]) -> int:
        assert 0 <= right <= self._n
        assert f(self._e)

        if right == 0:
            return 0

        right += self._size
        sm = self._e

        first = True
        while first or (right & -right) != right:
            first = False
            right -= 1
            while right > 1 and right % 2:
                right >>= 1
            if not f(self._op(self._d[right], sm)):
                while right < self._size:
                    right = 2 * right + 1
                    if f(self._op(self._d[right], sm)):
                        sm = self._op(self._d[right], sm)
                        right -= 1
                return right + 1 - self._size
            sm = self._op(self._d[right], sm)

        return 0

    def _update(self, k: int) -> None:
        self._d[k] = self._op(self._d[2 * k], self._d[2 * k + 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()

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

ans=[-1]*q
st=SegTree(max,-inf,n)
p=0
for x,l,r,i in xlri:
    while p<n and xj[p][0]<=x:
        x0,j=xj[p]
        st.set(j,x0)
        p+=1
    ans[i]=x-st.prod(l,r)

st=SegTree(min,inf,n)
p=n-1
for x,l,r,i in xlri[::-1]:
    while p>=0 and xj[p][0]>=x:
        x0,j=xj[p]
        st.set(j,x0)
        p-=1
    cur=st.prod(l,r)-x
    if cur<ans[i]:ans[i]=cur

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