結果

問題 No.2242 Cities and Teleporters
ユーザー navel_tosnavel_tos
提出日時 2023-05-19 19:05:37
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,509 bytes
コンパイル時間 868 ms
コンパイル使用メモリ 82,436 KB
実行使用メモリ 261,168 KB
最終ジャッジ日時 2024-05-10 03:59:55
合計ジャッジ時間 34,696 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
61,232 KB
testcase_01 AC 39 ms
52,952 KB
testcase_02 AC 40 ms
52,956 KB
testcase_03 AC 39 ms
52,676 KB
testcase_04 AC 39 ms
53,016 KB
testcase_05 AC 1,855 ms
186,980 KB
testcase_06 AC 1,415 ms
202,840 KB
testcase_07 AC 1,901 ms
192,328 KB
testcase_08 AC 2,410 ms
195,248 KB
testcase_09 AC 2,335 ms
192,532 KB
testcase_10 AC 1,994 ms
258,580 KB
testcase_11 AC 1,749 ms
256,136 KB
testcase_12 AC 1,634 ms
256,044 KB
testcase_13 AC 2,021 ms
258,408 KB
testcase_14 AC 2,244 ms
257,464 KB
testcase_15 AC 2,035 ms
258,816 KB
testcase_16 AC 2,197 ms
259,144 KB
testcase_17 AC 2,618 ms
259,084 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input=sys.stdin.readline
from bisect import bisect_left as bL, bisect_right as bR
f=lambda:list(map(int,input().split()))
N=int(input()); H,T=[f() for _ in range(2)]; Q=int(input())

#座標圧縮
S=set(H+T); D={j:i for i,j in enumerate(sorted(S))}
for i in range(N): H[i],T[i]=D[H[i]],D[T[i]]

#move[i]: 標高i以下のテレポーターを1回使うことで移動可能な最大の標高
P=sorted([(T[i],H[i]) for i in range(N)],reverse=True); move=[-1]*len(S); Lt=len(S)-1
for t,h in P:
    if h>t: move[h]=max(move[h],t); continue
    Lt=min(Lt,t)
    while Lt>=h: move[Lt]=t; Lt-=1
for i in range(1,len(S)): move[i]=max(move[i],move[i-1])

#DP[i][j]: 標高iから2**j回以内の移動を行うことで到達できる最大の標高(-1は移動不能)
#ここで、移動することで不利になる場合は移動しない点に注意せよ
DP=[[move[i]]+[-1]*19 for i in range(len(S))]
for j in range(1,20):
    for i in range(len(S)):
        if DP[i][j-1]==-1: continue
        DP[i][j]=max(DP[i][j-1],DP[DP[i][j-1]][j-1])

#クエリに解答
for _ in range(Q):
    A,B=f(); end=H[B-1]; now=T[A-1]; cnt=1  #初回の移動だけは例外で、T[now]に移動する
    #2**19回 ≒ 5e5回の移動でも届かないならば、到達不能と判断して良い
    if DP[H[A-1]][-1]<end: print(-1); continue
    #DP[now][j]のうち、end以下である最小のjを求め移動する
    while now<end:
        x=max(0,bL(DP[now],end)-1); cnt+=2**x; now=DP[now][x]
    print(cnt)
0