結果

問題 No.2242 Cities and Teleporters
ユーザー titia
提出日時 2023-03-11 02:35:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,050 ms / 3,000 ms
コード長 1,251 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 251,992 KB
最終ジャッジ日時 2024-09-18 06:06:54
合計ジャッジ時間 19,229 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N=int(input())

H=list(map(int,input().split()))
T=list(map(int,input().split()))

X=H+T+[0]
X=sorted(set(X))
D={X[i]:i for i in range(len(X))}

for i in range(N):
    H[i]=D[H[i]]
    T[i]=D[T[i]]

DOUBLING=[0]*len(X)

for i in range(N):
    DOUBLING[H[i]]=max(DOUBLING[H[i]],T[i])

for i in range(1,len(X)):
    DOUBLING[i]=max(DOUBLING[i],DOUBLING[i-1])

DOUBLING=[DOUBLING]

for i in range(30):
    j = 1<<i
    B = [0]*len(X)
    for k in range(len(X)):
        B[k]=DOUBLING[-1][DOUBLING[-1][k]]
    DOUBLING.append(B)

Q=int(input())
ANS=[-1]*Q
for tests in range(Q):
    x,y=map(int,input().split())
    x-=1
    y-=1

    start=T[x]
    to=H[y]

    if to<=start:
        ANS[tests]=1
        continue

    now=start
    score=0

    while now!=to:

        for i in range(30):
            if now<len(DOUBLING[i]) and DOUBLING[i][now]>=to:
                if i==0:
                    now=to
                    score+=1
                    break
                else:
                    now=DOUBLING[i-1][now]
                    score+=(1<<(i-1))
                    break
        else:
            ANS[tests]=-1
            break
    else:
        ANS[tests]=score+1

print("\n".join(map(str,ANS)))
0