結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2021-01-08 18:42:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,154 ms / 2,500 ms
コード長 3,864 bytes
コンパイル時間 133 ms
コンパイル使用メモリ 82,300 KB
実行使用メモリ 131,200 KB
最終ジャッジ日時 2024-04-27 19:31:02
合計ジャッジ時間 38,818 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
68,020 KB
testcase_01 AC 55 ms
67,712 KB
testcase_02 AC 55 ms
68,000 KB
testcase_03 AC 1,096 ms
120,020 KB
testcase_04 AC 1,088 ms
120,576 KB
testcase_05 AC 894 ms
120,064 KB
testcase_06 AC 588 ms
130,944 KB
testcase_07 AC 761 ms
130,816 KB
testcase_08 AC 583 ms
130,808 KB
testcase_09 AC 555 ms
130,672 KB
testcase_10 AC 565 ms
130,616 KB
testcase_11 AC 545 ms
130,560 KB
testcase_12 AC 543 ms
130,560 KB
testcase_13 AC 589 ms
130,560 KB
testcase_14 AC 573 ms
131,072 KB
testcase_15 AC 572 ms
130,620 KB
testcase_16 AC 1,151 ms
130,792 KB
testcase_17 AC 1,139 ms
131,020 KB
testcase_18 AC 1,032 ms
130,552 KB
testcase_19 AC 1,145 ms
130,680 KB
testcase_20 AC 1,116 ms
130,688 KB
testcase_21 AC 1,138 ms
130,944 KB
testcase_22 AC 1,152 ms
130,672 KB
testcase_23 AC 1,076 ms
130,432 KB
testcase_24 AC 942 ms
131,200 KB
testcase_25 AC 1,137 ms
130,432 KB
testcase_26 AC 400 ms
131,200 KB
testcase_27 AC 440 ms
130,816 KB
testcase_28 AC 217 ms
85,968 KB
testcase_29 AC 283 ms
84,992 KB
testcase_30 AC 330 ms
85,136 KB
testcase_31 AC 134 ms
87,808 KB
testcase_32 AC 340 ms
86,720 KB
testcase_33 AC 310 ms
85,668 KB
testcase_34 AC 172 ms
86,400 KB
testcase_35 AC 214 ms
86,144 KB
testcase_36 AC 202 ms
86,016 KB
testcase_37 AC 290 ms
84,948 KB
testcase_38 AC 763 ms
113,408 KB
testcase_39 AC 417 ms
87,400 KB
testcase_40 AC 1,154 ms
126,572 KB
testcase_41 AC 548 ms
95,820 KB
testcase_42 AC 855 ms
114,584 KB
testcase_43 AC 789 ms
105,600 KB
testcase_44 AC 1,030 ms
115,456 KB
testcase_45 AC 1,021 ms
119,916 KB
testcase_46 AC 871 ms
112,052 KB
testcase_47 AC 928 ms
118,076 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
#入力
N=int(input())
X=list(map(int,input().split()))
Q=int(input())
l=[0]*Q
r=[0]*Q
x=[0]*Q
for i in range(Q):
    l[i],r[i],x[i]=map(int,input().split())
#座圧
A=sorted(X)
#クエリ先読み
query_index=[[]for i in range(N)]
for i in range(Q):
    query_index[r[i]-1].append(i)
#本題
seg=SegTree([-1]*N,max,lambda: -1)
ans=[1000000000]*Q
for i in range(N):
    seg.set(bisect.bisect_left(A,X[i]),i)
    for j in query_index[i]:
        now=bisect.bisect_left(A,x[j])
        index=seg.max_right(now,lambda v: v<l[j]-1)
        if index<N:
            ans[j]=min(ans[j],A[index]-x[j])
        if now==0:
            continue
        index=seg.min_left(now,lambda v: v<l[j]-1)-1
        if index>=0:
            ans[j]=min(ans[j],x[j]-A[index])
for i in ans:
    print(i)




    
0