結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2021-01-08 18:56:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,070 ms / 2,500 ms
コード長 3,947 bytes
コンパイル時間 853 ms
コンパイル使用メモリ 82,308 KB
実行使用メモリ 167,772 KB
最終ジャッジ日時 2024-04-27 19:46:20
合計ジャッジ時間 56,389 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
68,380 KB
testcase_01 AC 65 ms
68,956 KB
testcase_02 AC 65 ms
68,580 KB
testcase_03 AC 1,800 ms
155,560 KB
testcase_04 AC 1,711 ms
155,616 KB
testcase_05 AC 1,736 ms
155,944 KB
testcase_06 AC 974 ms
163,564 KB
testcase_07 AC 994 ms
163,620 KB
testcase_08 AC 1,077 ms
164,108 KB
testcase_09 AC 982 ms
162,260 KB
testcase_10 AC 967 ms
163,860 KB
testcase_11 AC 1,023 ms
163,952 KB
testcase_12 AC 978 ms
163,628 KB
testcase_13 AC 1,000 ms
162,084 KB
testcase_14 AC 979 ms
163,628 KB
testcase_15 AC 966 ms
163,536 KB
testcase_16 AC 2,070 ms
167,188 KB
testcase_17 AC 1,891 ms
167,296 KB
testcase_18 AC 1,875 ms
167,612 KB
testcase_19 AC 2,003 ms
167,728 KB
testcase_20 AC 1,899 ms
167,624 KB
testcase_21 AC 1,886 ms
167,616 KB
testcase_22 AC 1,931 ms
167,764 KB
testcase_23 AC 1,908 ms
167,616 KB
testcase_24 AC 1,873 ms
167,772 KB
testcase_25 AC 1,880 ms
167,648 KB
testcase_26 AC 679 ms
155,676 KB
testcase_27 AC 692 ms
166,356 KB
testcase_28 AC 394 ms
93,772 KB
testcase_29 AC 428 ms
93,724 KB
testcase_30 AC 440 ms
94,432 KB
testcase_31 AC 295 ms
93,856 KB
testcase_32 AC 420 ms
94,380 KB
testcase_33 AC 420 ms
94,436 KB
testcase_34 AC 334 ms
93,672 KB
testcase_35 AC 384 ms
93,640 KB
testcase_36 AC 379 ms
93,648 KB
testcase_37 AC 394 ms
93,652 KB
testcase_38 AC 1,208 ms
139,280 KB
testcase_39 AC 569 ms
95,048 KB
testcase_40 AC 1,858 ms
167,380 KB
testcase_41 AC 787 ms
104,708 KB
testcase_42 AC 1,204 ms
141,492 KB
testcase_43 AC 887 ms
115,552 KB
testcase_44 AC 1,437 ms
138,564 KB
testcase_45 AC 1,349 ms
141,748 KB
testcase_46 AC 1,079 ms
133,544 KB
testcase_47 AC 1,322 ms
144,588 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())
l=[0]*Q
r=[0]*Q
x=[0]*Q
for i in range(Q):
    l[i],r[i],x[i]=map(int,input().split())
#sort
data=[[]for i in range(N+Q)]
for i in range(N):
    data[i]=[X[i],i]
for i in range(Q):
    data[i+N]=[x[i],i+N]
data.sort(key=lambda val:val[0])
#本題
ans=[-1]*Q
seg=SegTree([-1]*N,max,lambda: -1)
for i in data:
    if i[1]<N:
        seg.set(i[1],i[0])
    else:
        index=i[1]-N
        mem=seg.prod(l[index]-1,r[index])
        if mem!=-1:
            ans[index]=x[index]-mem
seg=SegTree([inf+1]*N,min,lambda: inf+1)
data.reverse()
for i in data:
    if i[1]<N:
        seg.set(i[1],i[0])
    else:
        index=i[1]-N
        mem=seg.prod(l[index]-1,r[index])
        if mem!=inf+1 and (ans[index]==-1 or ans[index]>mem-x[index]):
            ans[index]=mem-x[index]
for i in ans:
    print(i)

0