結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2020-12-31 06:29:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,463 ms / 2,500 ms
コード長 3,999 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 150,868 KB
最終ジャッジ日時 2024-04-17 07:55:01
合計ジャッジ時間 43,703 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
67,840 KB
testcase_01 AC 64 ms
68,096 KB
testcase_02 AC 66 ms
67,840 KB
testcase_03 AC 1,305 ms
131,840 KB
testcase_04 AC 1,306 ms
131,564 KB
testcase_05 AC 1,301 ms
131,892 KB
testcase_06 AC 757 ms
138,624 KB
testcase_07 AC 774 ms
138,512 KB
testcase_08 AC 757 ms
138,580 KB
testcase_09 AC 762 ms
138,880 KB
testcase_10 AC 759 ms
138,240 KB
testcase_11 AC 753 ms
138,752 KB
testcase_12 AC 746 ms
138,660 KB
testcase_13 AC 756 ms
138,624 KB
testcase_14 AC 750 ms
138,712 KB
testcase_15 AC 742 ms
138,736 KB
testcase_16 AC 1,448 ms
150,320 KB
testcase_17 AC 1,435 ms
150,596 KB
testcase_18 AC 1,430 ms
150,184 KB
testcase_19 AC 1,421 ms
150,724 KB
testcase_20 AC 1,443 ms
150,868 KB
testcase_21 AC 1,446 ms
150,844 KB
testcase_22 AC 1,463 ms
150,452 KB
testcase_23 AC 1,423 ms
150,048 KB
testcase_24 AC 1,425 ms
150,256 KB
testcase_25 AC 1,411 ms
150,444 KB
testcase_26 AC 551 ms
146,260 KB
testcase_27 AC 529 ms
138,200 KB
testcase_28 AC 328 ms
90,620 KB
testcase_29 AC 332 ms
90,704 KB
testcase_30 AC 338 ms
91,076 KB
testcase_31 AC 266 ms
90,300 KB
testcase_32 AC 353 ms
91,636 KB
testcase_33 AC 342 ms
92,012 KB
testcase_34 AC 293 ms
91,000 KB
testcase_35 AC 329 ms
90,892 KB
testcase_36 AC 331 ms
91,816 KB
testcase_37 AC 335 ms
90,392 KB
testcase_38 AC 894 ms
117,040 KB
testcase_39 AC 485 ms
90,084 KB
testcase_40 AC 1,436 ms
149,772 KB
testcase_41 AC 640 ms
103,448 KB
testcase_42 AC 863 ms
114,356 KB
testcase_43 AC 718 ms
102,824 KB
testcase_44 AC 1,160 ms
126,828 KB
testcase_45 AC 1,110 ms
133,436 KB
testcase_46 AC 816 ms
109,712 KB
testcase_47 AC 1,055 ms
129,384 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

inf=int(1e9)
N=int(input())
X=list(map(int,input().split()))
Q=int(input())
#data[i][1] が正ならクエリ、そうでなければ座標
data=[[] for i in range(N+Q)]
left=[0]*Q
right=[0]*Q
for i in range(N):
    data[i]=(X[i],-i)
for i in range(Q):
    left[i],right[i],x=map(int, input().split())
    data[i+N]=(x,i+1)
ans=[inf]*Q
A=[-inf]*N
seg=SegTree(A,max,lambda: -inf)
data.sort(key=lambda  val:val[0])
#前から
for i in range(N+Q):
    if data[i][1]<=0:
        seg.set(-data[i][1],data[i][0])
    else:
        index=data[i][1]-1
        ans[index]=min(ans[index],data[i][0]-seg.prod(left[index]-1,right[index]))
seg=SegTree(A,max,lambda : -inf)
#後ろから
for a in range(N+Q):
    i=N+Q-1-a
    if data[i][1]<=0:
        seg.set(-data[i][1],inf-data[i][0])
    else:
        index=data[i][1]-1
        ans[index]=min(ans[index],inf-data[i][0]-seg.prod(left[index]-1,right[index]))
for i in range(Q):
    print(ans[i])
0