結果

問題 No.1332 Range Nearest Query
ユーザー chineristACchineristAC
提出日時 2021-01-08 22:07:47
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,484 ms / 2,500 ms
コード長 1,879 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 87,236 KB
実行使用メモリ 150,480 KB
最終ジャッジ日時 2023-08-10 09:51:34
合計ジャッジ時間 44,769 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,568 KB
testcase_01 AC 71 ms
71,272 KB
testcase_02 AC 70 ms
71,068 KB
testcase_03 AC 1,387 ms
143,488 KB
testcase_04 AC 1,387 ms
143,516 KB
testcase_05 AC 1,430 ms
143,676 KB
testcase_06 AC 599 ms
145,620 KB
testcase_07 AC 592 ms
145,612 KB
testcase_08 AC 592 ms
145,796 KB
testcase_09 AC 606 ms
146,144 KB
testcase_10 AC 606 ms
145,840 KB
testcase_11 AC 593 ms
145,700 KB
testcase_12 AC 602 ms
146,332 KB
testcase_13 AC 607 ms
145,932 KB
testcase_14 AC 593 ms
145,628 KB
testcase_15 AC 597 ms
145,968 KB
testcase_16 AC 1,437 ms
149,804 KB
testcase_17 AC 1,423 ms
149,020 KB
testcase_18 AC 1,484 ms
150,316 KB
testcase_19 AC 1,432 ms
149,048 KB
testcase_20 AC 1,433 ms
149,940 KB
testcase_21 AC 1,429 ms
149,800 KB
testcase_22 AC 1,421 ms
149,860 KB
testcase_23 AC 1,467 ms
150,480 KB
testcase_24 AC 1,413 ms
148,804 KB
testcase_25 AC 1,423 ms
149,188 KB
testcase_26 AC 495 ms
142,216 KB
testcase_27 AC 329 ms
143,684 KB
testcase_28 AC 359 ms
99,876 KB
testcase_29 AC 383 ms
99,580 KB
testcase_30 AC 430 ms
101,124 KB
testcase_31 AC 295 ms
98,080 KB
testcase_32 AC 425 ms
98,972 KB
testcase_33 AC 421 ms
99,808 KB
testcase_34 AC 345 ms
99,784 KB
testcase_35 AC 384 ms
100,260 KB
testcase_36 AC 383 ms
100,648 KB
testcase_37 AC 395 ms
99,928 KB
testcase_38 AC 999 ms
119,040 KB
testcase_39 AC 669 ms
102,728 KB
testcase_40 AC 1,441 ms
147,636 KB
testcase_41 AC 734 ms
105,468 KB
testcase_42 AC 963 ms
114,904 KB
testcase_43 AC 879 ms
109,712 KB
testcase_44 AC 1,250 ms
138,696 KB
testcase_45 AC 1,201 ms
134,652 KB
testcase_46 AC 986 ms
112,856 KB
testcase_47 AC 1,075 ms
125,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:
    def __init__(self, init_val, segfunc, ide_ele):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

import sys

input = sys.stdin.readline

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

query.sort(key=lambda x:x[2])
ans = [0 for i in range(Q)]

Seg_lower = SegmentTree([-10**15 for i in range(N)],max,-10**15)
Seg_upper = SegmentTree([X[i] for i in range(N)],min,10**15)

val = [(X[i],i) for i in range(N)]
val.sort(key=lambda x:-x[0])

for l,r,x,idx in query:
    while val and val[-1][0] <= x:
        Seg_lower.update(val[-1][1],val[-1][0])
        Seg_upper.update(val[-1][1],10**15)
        val.pop()

    lower_tmp = x - Seg_lower.query(l,r)
    upper_tmp = Seg_upper.query(l,r) - x
    ans[idx] = min(lower_tmp,upper_tmp)
    #print(lower_tmp,upper_tmp)

print(*ans,sep="\n")
0