結果

問題 No.1332 Range Nearest Query
ユーザー wolgnikwolgnik
提出日時 2021-01-08 23:13:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,803 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 174,704 KB
最終ジャッジ日時 2024-04-28 05:19:59
合計ジャッジ時間 55,069 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,744 KB
testcase_01 AC 34 ms
54,128 KB
testcase_02 AC 34 ms
54,192 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 613 ms
160,384 KB
testcase_07 AC 593 ms
160,684 KB
testcase_08 AC 589 ms
161,252 KB
testcase_09 AC 590 ms
160,324 KB
testcase_10 AC 599 ms
160,820 KB
testcase_11 AC 592 ms
160,756 KB
testcase_12 AC 612 ms
160,408 KB
testcase_13 AC 597 ms
151,356 KB
testcase_14 AC 591 ms
160,604 KB
testcase_15 AC 591 ms
160,704 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 403 ms
161,908 KB
testcase_27 AC 388 ms
152,260 KB
testcase_28 AC 279 ms
83,572 KB
testcase_29 AC 325 ms
84,396 KB
testcase_30 AC 363 ms
85,176 KB
testcase_31 AC 186 ms
83,584 KB
testcase_32 AC 371 ms
84,544 KB
testcase_33 AC 366 ms
85,008 KB
testcase_34 AC 240 ms
83,700 KB
testcase_35 AC 293 ms
83,836 KB
testcase_36 AC 299 ms
84,016 KB
testcase_37 AC 317 ms
84,168 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 #

import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
Q = int(input())
ls = [0] * Q
rs = [0] * Q
xs = [0] * Q
qs = []

for i in range(Q):
  l, r, x = map(int, input().split())
  l -= 1
  ls[i] = l
  rs[i] = r
  xs[i] = x
  qs.append(i)
qs.sort(key = lambda x: xs[x])


N0 = 2**(N-1).bit_length()
INF = 10 ** 9
data = [0]*(2*N0)
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = max(data[2*k+1], data[2*k+2])
def query(l, r):
    L = l + N0; R = r + N0
    s = 0
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])

        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

s = []
for i in range(N): s.append((a[i], i))
s.sort(reverse = True)

res = [INF] * Q
for i in qs:
  l, r, x = ls[i], rs[i], xs[i]
  while len(s) and s[-1][0] <= x:
    y, j = s.pop()
    update(j, y)
  q = query(l, r)
  if q: res[i] = x - q
#print(res)


# N: 処理する区間の長さ
N0 = 2**(N-1).bit_length()
data = [INF]*(2*N0)
# a_k の値を x に更新
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = min(data[2*k+1], data[2*k+2])
# 区間[l, r)の最小値
def query(l, r):
    L = l + N0; R = r + N0
    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = min(s, data[R-1])

        if L & 1:
            s = min(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

s = []
for i in range(N): s.append((a[i], i))
s.sort()
qs.reverse()

for i in qs:
  l, r, x = ls[i], rs[i], xs[i]
  while len(s) and s[-1][0] >= x:
    y, j = s.pop()
    update(j, y)
  q = query(l, r)
  if q: res[i] = min(res[i], q - x)

for r in res: print(r)
0