結果

問題 No.2242 Cities and Teleporters
ユーザー titia
提出日時 2023-03-11 02:05:42
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,264 bytes
コンパイル時間 224 ms
コンパイル使用メモリ 82,264 KB
実行使用メモリ 83,132 KB
最終ジャッジ日時 2024-09-18 06:03:28
合計ジャッジ時間 6,018 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 TLE * 1 -- * 21
権限があれば一括ダウンロードができます

ソースコード

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
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=[-1]*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 = []
    for k in range(len(DOUBLING[-1])-j):
        B.append(max(DOUBLING[-1][k], DOUBLING[-1][k+j]))
    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)
                    break
        else:
            ANS[tests]=-1
            break
    else:
        ANS[tests]=score+1

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