結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2020-11-04 06:39:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,494 ms / 2,500 ms
コード長 3,531 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 187,828 KB
最終ジャッジ日時 2024-07-22 10:40:13
合計ジャッジ時間 69,817 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,376 KB
testcase_01 AC 44 ms
52,736 KB
testcase_02 AC 45 ms
52,864 KB
testcase_03 AC 2,358 ms
178,052 KB
testcase_04 AC 2,464 ms
181,948 KB
testcase_05 AC 2,294 ms
178,056 KB
testcase_06 AC 1,197 ms
176,048 KB
testcase_07 AC 1,195 ms
175,976 KB
testcase_08 AC 1,200 ms
176,008 KB
testcase_09 AC 1,213 ms
175,992 KB
testcase_10 AC 1,211 ms
176,556 KB
testcase_11 AC 1,195 ms
176,560 KB
testcase_12 AC 1,190 ms
176,004 KB
testcase_13 AC 1,224 ms
177,388 KB
testcase_14 AC 1,199 ms
177,036 KB
testcase_15 AC 1,209 ms
176,028 KB
testcase_16 AC 2,476 ms
187,828 KB
testcase_17 AC 2,304 ms
186,732 KB
testcase_18 AC 2,494 ms
185,420 KB
testcase_19 AC 2,308 ms
186,020 KB
testcase_20 AC 2,411 ms
186,976 KB
testcase_21 AC 2,395 ms
185,756 KB
testcase_22 AC 2,436 ms
186,532 KB
testcase_23 AC 2,155 ms
185,416 KB
testcase_24 AC 2,205 ms
186,144 KB
testcase_25 AC 2,399 ms
186,036 KB
testcase_26 AC 826 ms
169,612 KB
testcase_27 AC 820 ms
168,932 KB
testcase_28 AC 297 ms
87,320 KB
testcase_29 AC 370 ms
87,440 KB
testcase_30 AC 384 ms
85,808 KB
testcase_31 AC 196 ms
88,740 KB
testcase_32 AC 390 ms
86,508 KB
testcase_33 AC 385 ms
86,416 KB
testcase_34 AC 260 ms
87,808 KB
testcase_35 AC 306 ms
87,392 KB
testcase_36 AC 298 ms
87,768 KB
testcase_37 AC 380 ms
86,856 KB
testcase_38 AC 1,622 ms
130,628 KB
testcase_39 AC 665 ms
89,840 KB
testcase_40 AC 2,402 ms
184,760 KB
testcase_41 AC 1,067 ms
102,844 KB
testcase_42 AC 1,560 ms
128,708 KB
testcase_43 AC 1,627 ms
118,240 KB
testcase_44 AC 2,030 ms
161,092 KB
testcase_45 AC 1,892 ms
149,500 KB
testcase_46 AC 1,688 ms
121,896 KB
testcase_47 AC 2,046 ms
136,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Refernce: https://atcoder.jp/contests/practice2/submissions/16814531
class SegTree():
    def __init__(self, a, func, idem):
        # the number of nodes is 2n - 1
        self.n = 1 << len(a).bit_length()
        self.size = len(a)
        self.func = func
        self.idem = idem # Identity element
        self.node = [idem] * (2 * self.n - 1)
        for i in range(len(a) + self.n - 2, -1, -1):
            if i >= self.n - 1:
                self.node[i] = a[i - self.n + 1]
            else:
                self.node[i] = self.func(self.node[2 * i + 1], self.node[2 * i + 2])
            
    def get(self, x):
        return self.node[x + self.n - 1]

    def set(self, x, val):
        x += self.n - 1
        self.node[x] = val
        while x > 0:
            x = (x - 1) >> 1
            self.node[x] = self.func(self.node[2*x+1], self.node[2*x+2])
        return
    
    def prod(self, l, r):
        L, R = l + self.n, r + self.n
        s_l, s_r = self.idem, self.idem
        while L < R:
            if R & 1:
                R -= 1
                s_r = self.func(self.node[R-1], s_r)
            if L & 1:
                s_l = s_l = self.func(s_l, self.node[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return self.func(s_l, s_r)

    def max_right(self, l, val):
        if l == self.size:
            return self.size
        L = l + self.n
        s = self.idem
        while 1:
            while L % 2 == 0:
                L >>= 1
            if not val(self.func(s, self.node[L - 1])):
                while L < self.n:
                    L <<= 1
                    if val(self.func(s, self.node[L-1])):
                        s = self.func(s, self.node[L-1])
                        L += 1
                return L - self.n
            s = self.func(s, self.node[L - 1])
            L += 1
            if (L & -L) == L:
                return self.size

    def min_left(self, r, val):
        if r == 0:
            return 0
        R = r + self.n
        s = self.idem
        while True:
            R -= 1
            while R > 1 and R % 2:
                R >>= 1
            if not val(self.func(self.node[R - 1], s)):
                while R < self.n:
                    R = (R << 1) + 1
                    if val(self.func(self.node[R - 1], s)):
                        s = self.func(self.node[R - 1], s)
                        R -= 1
                return R + 1 - self.n
            s = self.func(self.node[R - 1], s)
            if (R & -R) == R:
                return 0

import sys
input=sys.stdin.readline
import bisect

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