結果

問題 No.1332 Range Nearest Query
ユーザー Kiri8128Kiri8128
提出日時 2021-01-08 23:21:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,723 ms / 2,500 ms
コード長 3,387 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 181,512 KB
最終ジャッジ日時 2024-11-16 18:13:24
合計ジャッジ時間 48,978 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,736 KB
testcase_01 AC 36 ms
52,480 KB
testcase_02 AC 35 ms
52,992 KB
testcase_03 AC 1,461 ms
167,484 KB
testcase_04 AC 1,463 ms
167,476 KB
testcase_05 AC 1,478 ms
167,476 KB
testcase_06 AC 824 ms
179,908 KB
testcase_07 AC 829 ms
180,188 KB
testcase_08 AC 829 ms
179,824 KB
testcase_09 AC 803 ms
180,072 KB
testcase_10 AC 824 ms
180,064 KB
testcase_11 AC 817 ms
180,060 KB
testcase_12 AC 811 ms
180,156 KB
testcase_13 AC 842 ms
180,004 KB
testcase_14 AC 896 ms
180,068 KB
testcase_15 AC 829 ms
180,528 KB
testcase_16 AC 1,673 ms
181,292 KB
testcase_17 AC 1,723 ms
180,828 KB
testcase_18 AC 1,671 ms
180,876 KB
testcase_19 AC 1,669 ms
180,968 KB
testcase_20 AC 1,705 ms
181,204 KB
testcase_21 AC 1,701 ms
181,512 KB
testcase_22 AC 1,685 ms
180,780 KB
testcase_23 AC 1,708 ms
180,840 KB
testcase_24 AC 1,710 ms
181,148 KB
testcase_25 AC 1,688 ms
180,860 KB
testcase_26 AC 569 ms
177,980 KB
testcase_27 AC 537 ms
169,484 KB
testcase_28 AC 327 ms
94,316 KB
testcase_29 AC 346 ms
94,068 KB
testcase_30 AC 339 ms
93,808 KB
testcase_31 AC 251 ms
91,892 KB
testcase_32 AC 359 ms
94,408 KB
testcase_33 AC 346 ms
94,024 KB
testcase_34 AC 291 ms
93,672 KB
testcase_35 AC 324 ms
93,808 KB
testcase_36 AC 326 ms
93,808 KB
testcase_37 AC 346 ms
94,060 KB
testcase_38 AC 1,084 ms
132,064 KB
testcase_39 AC 491 ms
97,824 KB
testcase_40 AC 1,642 ms
180,676 KB
testcase_41 AC 648 ms
106,432 KB
testcase_42 AC 989 ms
124,736 KB
testcase_43 AC 803 ms
112,720 KB
testcase_44 AC 1,302 ms
147,900 KB
testcase_45 AC 1,222 ms
142,020 KB
testcase_46 AC 953 ms
118,648 KB
testcase_47 AC 1,176 ms
132,636 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