結果

問題 No.1332 Range Nearest Query
ユーザー penguinmanpenguinman
提出日時 2020-11-23 05:13:01
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,492 bytes
コンパイル時間 355 ms
コンパイル使用メモリ 86,912 KB
実行使用メモリ 188,580 KB
最終ジャッジ日時 2023-09-30 23:30:51
合計ジャッジ時間 70,923 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,244 KB
testcase_01 AC 73 ms
71,040 KB
testcase_02 AC 74 ms
70,888 KB
testcase_03 WA -
testcase_04 TLE -
testcase_05 WA -
testcase_06 AC 1,113 ms
185,976 KB
testcase_07 AC 1,132 ms
186,000 KB
testcase_08 AC 1,121 ms
185,908 KB
testcase_09 AC 1,100 ms
186,320 KB
testcase_10 AC 1,107 ms
186,068 KB
testcase_11 AC 1,104 ms
185,552 KB
testcase_12 AC 1,113 ms
185,656 KB
testcase_13 AC 1,105 ms
185,676 KB
testcase_14 AC 1,124 ms
185,496 KB
testcase_15 AC 1,145 ms
185,000 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 678 ms
183,620 KB
testcase_27 AC 681 ms
183,980 KB
testcase_28 AC 475 ms
91,040 KB
testcase_29 AC 477 ms
91,400 KB
testcase_30 AC 497 ms
91,184 KB
testcase_31 AC 402 ms
90,520 KB
testcase_32 AC 499 ms
91,136 KB
testcase_33 AC 513 ms
91,700 KB
testcase_34 AC 454 ms
91,028 KB
testcase_35 AC 496 ms
91,172 KB
testcase_36 AC 489 ms
90,888 KB
testcase_37 AC 481 ms
91,264 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

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())
X=list(map(int,input().split()))
Q=int(input())
#data[i][1] が正ならクエリ、そうでなければ座標
data=[[0]*2 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],data[i+N][0]=map(int, input().split())
    data[i+N][1]=i+1
ans=[inf]*Q
A=[-inf]*N
seg=SegTree(A,max,-inf)
data.sort()
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]))
A=[inf]*N
seg=SegTree(A,min,inf)
for a in range(N+Q):
    i=N+Q-1-a
    if data[i][1]<=0:
        seg.set(-data[i][1],data[i][0])
    else:
        index=data[i][1]-1
        ans[index]=min(ans[index],seg.prod(left[index]-1,right[index])-data[i][0])
for i in range(Q):
    print(ans[i])
0