結果

問題 No.1332 Range Nearest Query
ユーザー chineristACchineristAC
提出日時 2021-01-08 22:07:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,556 ms / 2,500 ms
コード長 1,879 bytes
コンパイル時間 201 ms
コンパイル使用メモリ 81,980 KB
実行使用メモリ 149,008 KB
最終ジャッジ日時 2024-04-28 03:16:58
合計ジャッジ時間 45,204 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,352 KB
testcase_01 AC 40 ms
52,352 KB
testcase_02 AC 40 ms
52,224 KB
testcase_03 AC 1,460 ms
142,108 KB
testcase_04 AC 1,430 ms
142,036 KB
testcase_05 AC 1,455 ms
142,880 KB
testcase_06 AC 589 ms
144,376 KB
testcase_07 AC 576 ms
144,752 KB
testcase_08 AC 591 ms
144,240 KB
testcase_09 AC 602 ms
144,624 KB
testcase_10 AC 592 ms
144,240 KB
testcase_11 AC 577 ms
144,244 KB
testcase_12 AC 591 ms
144,240 KB
testcase_13 AC 603 ms
144,884 KB
testcase_14 AC 594 ms
144,248 KB
testcase_15 AC 614 ms
144,880 KB
testcase_16 AC 1,484 ms
148,620 KB
testcase_17 AC 1,495 ms
148,744 KB
testcase_18 AC 1,556 ms
148,088 KB
testcase_19 AC 1,494 ms
148,492 KB
testcase_20 AC 1,480 ms
148,492 KB
testcase_21 AC 1,478 ms
148,616 KB
testcase_22 AC 1,496 ms
148,620 KB
testcase_23 AC 1,512 ms
149,008 KB
testcase_24 AC 1,487 ms
148,484 KB
testcase_25 AC 1,486 ms
148,492 KB
testcase_26 AC 495 ms
142,360 KB
testcase_27 AC 333 ms
144,184 KB
testcase_28 AC 350 ms
99,192 KB
testcase_29 AC 363 ms
99,960 KB
testcase_30 AC 407 ms
100,348 KB
testcase_31 AC 266 ms
94,760 KB
testcase_32 AC 418 ms
100,012 KB
testcase_33 AC 399 ms
99,768 KB
testcase_34 AC 333 ms
99,444 KB
testcase_35 AC 364 ms
99,836 KB
testcase_36 AC 364 ms
99,952 KB
testcase_37 AC 379 ms
100,160 KB
testcase_38 AC 1,006 ms
115,588 KB
testcase_39 AC 661 ms
100,096 KB
testcase_40 AC 1,519 ms
147,560 KB
testcase_41 AC 727 ms
103,276 KB
testcase_42 AC 990 ms
114,528 KB
testcase_43 AC 907 ms
106,868 KB
testcase_44 AC 1,277 ms
136,408 KB
testcase_45 AC 1,192 ms
127,732 KB
testcase_46 AC 998 ms
109,516 KB
testcase_47 AC 1,087 ms
117,872 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