結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2020-11-04 07:05:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,261 ms / 2,500 ms
コード長 3,462 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 82,228 KB
実行使用メモリ 142,616 KB
最終ジャッジ日時 2024-07-22 10:47:39
合計ジャッジ時間 38,069 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,504 KB
testcase_01 AC 43 ms
52,736 KB
testcase_02 AC 39 ms
53,504 KB
testcase_03 AC 1,145 ms
131,100 KB
testcase_04 AC 940 ms
128,928 KB
testcase_05 AC 1,135 ms
132,180 KB
testcase_06 AC 630 ms
142,184 KB
testcase_07 AC 627 ms
142,188 KB
testcase_08 AC 648 ms
142,100 KB
testcase_09 AC 621 ms
142,228 KB
testcase_10 AC 633 ms
142,056 KB
testcase_11 AC 624 ms
142,480 KB
testcase_12 AC 604 ms
142,060 KB
testcase_13 AC 664 ms
142,616 KB
testcase_14 AC 631 ms
142,484 KB
testcase_15 AC 618 ms
142,100 KB
testcase_16 AC 1,235 ms
142,224 KB
testcase_17 AC 1,163 ms
142,236 KB
testcase_18 AC 1,036 ms
142,560 KB
testcase_19 AC 1,183 ms
142,232 KB
testcase_20 AC 1,222 ms
142,104 KB
testcase_21 AC 1,221 ms
142,104 KB
testcase_22 AC 1,246 ms
142,108 KB
testcase_23 AC 1,261 ms
142,108 KB
testcase_24 AC 1,240 ms
142,228 KB
testcase_25 AC 1,213 ms
142,344 KB
testcase_26 AC 399 ms
141,620 KB
testcase_27 AC 443 ms
142,008 KB
testcase_28 AC 240 ms
82,324 KB
testcase_29 AC 348 ms
83,792 KB
testcase_30 AC 364 ms
83,736 KB
testcase_31 AC 138 ms
81,944 KB
testcase_32 AC 319 ms
82,940 KB
testcase_33 AC 304 ms
82,416 KB
testcase_34 AC 199 ms
82,420 KB
testcase_35 AC 237 ms
82,048 KB
testcase_36 AC 239 ms
82,028 KB
testcase_37 AC 333 ms
83,496 KB
testcase_38 AC 998 ms
103,424 KB
testcase_39 AC 442 ms
84,536 KB
testcase_40 AC 1,179 ms
136,204 KB
testcase_41 AC 516 ms
89,224 KB
testcase_42 AC 824 ms
101,416 KB
testcase_43 AC 796 ms
96,448 KB
testcase_44 AC 1,052 ms
121,696 KB
testcase_45 AC 1,012 ms
116,572 KB
testcase_46 AC 858 ms
101,596 KB
testcase_47 AC 956 ms
105,084 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=int(1e9)
N=int(input())
A=[-inf]*N
X=list(map(int,input().split()))
Q=int(input())
ind=[[]for i in range(N)]
lef=[0]*Q
po=[0]*Q
for i in range(Q):
    l,r,x=map(int, input().split())
    ind[r-1].append(i)
    lef[i]=l-1
    po[i]=x
ans=[inf]*Q
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[index]
        point=po[index]
        right=i
        now=bisect.bisect_left(Y,point)
        index2=seg.max_right(now,lambda  z: z<left)
        if index2<N and ans[index]>Y[index2]-point:
            ans[index]=Y[index2]-point
        index2=seg.min_left(now,lambda  z: z<left)
        index2-=1
        if index2>=0 and ans[index]>point-Y[index2]:
            ans[index]=point-Y[index2]
for i in range(Q):
    print(ans[i])
0