結果

問題 No.2242 Cities and Teleporters
ユーザー navel_tosnavel_tos
提出日時 2023-05-19 18:57:46
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,819 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 82,564 KB
実行使用メモリ 269,540 KB
最終ジャッジ日時 2024-05-10 03:55:26
合計ジャッジ時間 43,435 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
58,112 KB
testcase_01 AC 40 ms
52,608 KB
testcase_02 AC 42 ms
52,864 KB
testcase_03 AC 40 ms
52,352 KB
testcase_04 AC 43 ms
52,608 KB
testcase_05 AC 2,433 ms
194,388 KB
testcase_06 AC 1,991 ms
198,904 KB
testcase_07 AC 2,507 ms
190,848 KB
testcase_08 AC 2,913 ms
192,612 KB
testcase_09 AC 2,824 ms
192,492 KB
testcase_10 AC 2,728 ms
267,600 KB
testcase_11 AC 2,258 ms
263,492 KB
testcase_12 AC 2,288 ms
263,684 KB
testcase_13 AC 2,586 ms
263,472 KB
testcase_14 AC 2,839 ms
264,016 KB
testcase_15 AC 2,597 ms
263,984 KB
testcase_16 AC 2,934 ms
263,232 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#yukicoder380D Cities and Teleporters

'''
下方向には自由に移動できるが、上方向への移動は限られる、というのが重要かな。
上昇方向のテレポーターのみを抽出して考えよう。

各町から最高の標高に移動する方法、を考えればよいのか。
とりあえず標高は座圧しよう、4*10**5通りまで減らせる。

事前の各標高に対して「この町から移動できる最大の標高」を記録しよう。
各町に対して、ではない点に注意。
後はダブリングかな。2**19 = 5.2e5 なので、ダブリング回数は20回で済む。
DP[i][j]: 町iから2**j回の移動で到達できる最大の標高
として、jが小さい順に埋めればよい。そのためには事前準備が必要か。大変だな。

テレポーターの移動先を降順ソートして、貪欲にあてはめればよいのかな。
移動ルールが Hi→Ti だったよな。
(Ti,Hi)の順にソートして、Tiを降順に見てゆく。
Hi> Ti は無視する。
Hi<=Ti で、Tiが固有値なら、区間[Hi,Ti]はTiにしてよい。
       Tiが同率なら、Hiが大きい順に見てゆけばOK。
「この標高まで更新した」のカーソルLtを保持しながら見れば大丈夫かな。
'''
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([i for i in H]+[i for i in T]); D={j:i for i,j in enumerate(sorted(S))}
H=[D[i] for i in H]; T=[D[i] for i in T]

#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]*20 for i in range(len(S))]
for j in range(1,21):
    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(); now=H[A-1]; end=H[B-1]
    #2**20回 ≒ 1e6回の移動でも届かないならば、到達不能と判断して良い
    if DP[now][-1]<end: print(-1); continue
    #初回の移動だけは例外で、T[now]に移動する
    now=T[A-1]; cnt=1
    #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