結果

問題 No.1332 Range Nearest Query
ユーザー Kiri8128Kiri8128
提出日時 2021-01-08 23:21:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,044 ms / 2,500 ms
コード長 3,387 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 181,712 KB
最終ジャッジ日時 2024-04-28 05:38:32
合計ジャッジ時間 57,516 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,352 KB
testcase_01 AC 43 ms
52,608 KB
testcase_02 AC 42 ms
52,352 KB
testcase_03 AC 1,815 ms
167,228 KB
testcase_04 AC 1,808 ms
167,484 KB
testcase_05 AC 1,841 ms
167,736 KB
testcase_06 AC 991 ms
180,068 KB
testcase_07 AC 961 ms
180,188 KB
testcase_08 AC 965 ms
180,316 KB
testcase_09 AC 928 ms
179,852 KB
testcase_10 AC 967 ms
180,092 KB
testcase_11 AC 941 ms
179,940 KB
testcase_12 AC 971 ms
179,936 KB
testcase_13 AC 968 ms
179,916 KB
testcase_14 AC 955 ms
180,072 KB
testcase_15 AC 949 ms
179,940 KB
testcase_16 AC 2,029 ms
181,712 KB
testcase_17 AC 2,016 ms
181,068 KB
testcase_18 AC 2,025 ms
181,216 KB
testcase_19 AC 2,038 ms
181,140 KB
testcase_20 AC 2,017 ms
180,808 KB
testcase_21 AC 2,044 ms
181,440 KB
testcase_22 AC 2,020 ms
180,928 KB
testcase_23 AC 1,979 ms
180,936 KB
testcase_24 AC 2,022 ms
181,432 KB
testcase_25 AC 2,027 ms
181,580 KB
testcase_26 AC 644 ms
177,484 KB
testcase_27 AC 599 ms
169,816 KB
testcase_28 AC 378 ms
94,196 KB
testcase_29 AC 398 ms
94,704 KB
testcase_30 AC 398 ms
94,064 KB
testcase_31 AC 297 ms
91,512 KB
testcase_32 AC 403 ms
94,156 KB
testcase_33 AC 414 ms
93,764 KB
testcase_34 AC 346 ms
94,136 KB
testcase_35 AC 380 ms
93,940 KB
testcase_36 AC 379 ms
94,060 KB
testcase_37 AC 393 ms
93,940 KB
testcase_38 AC 1,275 ms
132,168 KB
testcase_39 AC 588 ms
98,080 KB
testcase_40 AC 1,945 ms
180,476 KB
testcase_41 AC 784 ms
106,308 KB
testcase_42 AC 1,193 ms
124,356 KB
testcase_43 AC 960 ms
112,888 KB
testcase_44 AC 1,605 ms
148,156 KB
testcase_45 AC 1,486 ms
141,852 KB
testcase_46 AC 1,132 ms
119,032 KB
testcase_47 AC 1,445 ms
132,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()

class SegmentTree():
    def __init__(self, init, unitX, f):
        self.f = f # (X, X) -> X
        self.unitX = unitX
        self.f = f
        if type(init) == int:
            self.n = init
            self.n = 1 << (self.n - 1).bit_length()
            self.X = [unitX] * (self.n * 2)
        else:
            self.n = len(init)
            self.n = 1 << (self.n - 1).bit_length()
            self.X = [unitX] * self.n + init + [unitX] * (self.n - len(init))
            for i in range(self.n-1, 0, -1):
                self.X[i] = self.f(self.X[i*2], self.X[i*2|1])
        
    def update(self, i, x):
        i += self.n
        self.X[i] = x
        i >>= 1
        while i:
            self.X[i] = self.f(self.X[i*2], self.X[i*2|1])
            i >>= 1
    
    def getvalue(self, i):
        return self.X[i + self.n]
    
    def getrange(self, l, r):
        l += self.n
        r += self.n
        al = self.unitX
        ar = self.unitX
        while l < r:
            if l & 1:
                al = self.f(al, self.X[l])
                l += 1
            if r & 1:
                r -= 1
                ar = self.f(self.X[r], ar)
            l >>= 1
            r >>= 1
        return self.f(al, ar)
    
    # Find r s.t. calc(l, ..., r-1) = True and calc(l, ..., r) = False
    def max_right(self, l, z):
        if l >= self.n: return self.n
        l += self.n
        s = self.unitX
        while 1:
            while l % 2 == 0:
                l >>= 1
            if not z(self.f(s, self.X[l])):
                while l < self.n:
                    l *= 2
                    if z(self.f(s, self.X[l])):
                        s = self.f(s, self.X[l])
                        l += 1
                return l - self.n
            s = self.f(s, self.X[l])
            l += 1
            if l & -l == l: break
        return self.n
    
    # Find l s.t. calc(l, ..., r-1) = True and calc(l-1, ..., r-1) = False
    def min_left(self, r, z):
        if r <= 0: return 0
        r += self.n
        s = self.unitX
        while 1:
            r -= 1
            while r > 1 and r % 2:
                r >>= 1
            if not z(self.f(self.X[r], s)):
                while r < self.n:
                    r = r * 2 + 1
                    if z(self.f(self.X[r], s)):
                        s = self.f(self.X[r], s)
                        r -= 1
                return r + 1 - self.n
            s = self.f(self.X[r], s)
            if r & -r == r: break
        return 0
    
    def debug(self):
        print("debug")
        print([self.getvalue(i) for i in range(min(self.n, 20))])

N = int(input())
X = [int(a) for a in input().split()]
Y = []
Q = int(input())
for i in range(Q):
    l, r, x = map(int, input().split())
    Y.append((l-1, r, x, i))

for i, x in enumerate(X):
    Y.append((-1, -1, x, i))
Y.sort(key = lambda x: x[2])

ANS = [10 ** 9] * Q
f = max
unit = -10 ** 9
st = SegmentTree(N + 2, unit, f)    

for l, r, x, i in Y:
    if l >= 0:
        m = st.getrange(l, r)
        ANS[i] = min(ANS[i], x - m)
    else:
        st.update(i, x)

f = min
unit = 10 ** 9 * 2
st = SegmentTree(N + 2, unit, f)    

for l, r, x, i in Y[::-1]:
    if l >= 0:
        m = st.getrange(l, r)
        ANS[i] = min(ANS[i], m - x)
    else:
        st.update(i, x)
print("\n".join(map(str, ANS)))
0