結果

問題 No.1332 Range Nearest Query
ユーザー wolgnikwolgnik
提出日時 2021-01-08 23:07:29
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,832 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,484 KB
実行使用メモリ 174,916 KB
最終ジャッジ日時 2024-04-28 05:00:07
合計ジャッジ時間 25,055 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
54,344 KB
testcase_01 AC 48 ms
52,608 KB
testcase_02 AC 43 ms
52,608 KB
testcase_03 AC 2,396 ms
146,228 KB
testcase_04 AC 2,399 ms
146,820 KB
testcase_05 AC 2,343 ms
146,944 KB
testcase_06 AC 738 ms
160,420 KB
testcase_07 AC 687 ms
160,628 KB
testcase_08 AC 679 ms
161,068 KB
testcase_09 AC 695 ms
160,428 KB
testcase_10 AC 685 ms
160,556 KB
testcase_11 AC 675 ms
160,812 KB
testcase_12 AC 686 ms
160,428 KB
testcase_13 AC 694 ms
152,100 KB
testcase_14 AC 678 ms
160,424 KB
testcase_15 AC 691 ms
160,812 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 445 ms
162,044 KB
testcase_27 AC 439 ms
152,400 KB
testcase_28 AC 325 ms
84,176 KB
testcase_29 AC 374 ms
84,772 KB
testcase_30 AC 426 ms
85,556 KB
testcase_31 AC 217 ms
83,732 KB
testcase_32 AC 422 ms
84,540 KB
testcase_33 AC 436 ms
84,884 KB
testcase_34 AC 287 ms
84,116 KB
testcase_35 AC 351 ms
84,108 KB
testcase_36 AC 358 ms
83,728 KB
testcase_37 AC 376 ms
84,360 KB
testcase_38 AC 1,585 ms
115,008 KB
testcase_39 AC 675 ms
93,788 KB
testcase_40 TLE -
testcase_41 AC 882 ms
95,620 KB
testcase_42 AC 1,481 ms
113,060 KB
testcase_43 AC 1,164 ms
100,852 KB
testcase_44 AC 2,105 ms
133,248 KB
testcase_45 AC 2,002 ms
131,992 KB
testcase_46 AC 1,374 ms
106,460 KB
testcase_47 AC 1,771 ms
118,244 KB
権限があれば一括ダウンロードができます

ソースコード

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 = 2**31-1
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)

inf = 10 ** 10
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()
INF = 2**31-1
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