結果
| 問題 |
No.2242 Cities and Teleporters
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-01-14 00:46:23 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 2,026 ms / 3,000 ms |
| コード長 | 2,656 bytes |
| コンパイル時間 | 2,125 ms |
| コンパイル使用メモリ | 82,048 KB |
| 実行使用メモリ | 245,432 KB |
| 最終ジャッジ日時 | 2025-01-14 00:47:16 |
| 合計ジャッジ時間 | 36,758 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
## https://yukicoder.me/problems/no/2242
MAX_INT = 10 ** 18
def main():
N = int(input())
H = list(map(int, input().split()))
T = list(map(int, input().split()))
Q = int(input())
ab = []
for _ in range(Q):
a,b = map(int, input().split())
ab.append((a - 1, b - 1))
# Hについてソート
ht_array = [(H[i], T[i], i) for i in range(N)]
ht_array.sort(key=lambda x : x[0])
cum_ht_array = []
cum_t = 0
cum_i = 0
for i in range(N):
h0, t0, i0 = ht_array[i]
if cum_t < t0:
cum_t = max(cum_t, t0)
cum_i = i0
cum_ht_array.append((h0, cum_t, cum_i))
# 各Tについて「1発で行ける場所と次にいける場所のなかで最大のものをもとめる
t_set = set(T)
t_list = list(t_set)
t_list.sort(reverse=True)
t_map = {}
for i, t in enumerate(t_list):
t_map[t] = i
cum_ht_index = len(cum_ht_array) - 1
t_next_map = [-1] * len(t_list)
next_parents = [-1] * len(t_list)
for t in t_list:
while cum_ht_index >= 0 and cum_ht_array[cum_ht_index][0] > t:
cum_ht_index -= 1
if cum_ht_index >= 0:
h0, ct, _ = cum_ht_array[cum_ht_index]
if ct <= t:
t_next_map[t_map[t]] = h0
next_parents[t_map[t]] = -1
else:
t_next_map[t_map[t]] = h0
next_parents[t_map[t]] = t_map[ct]
# ダブリングの準備のため
max_k = 0
while (1 << max_k) < len(t_list):
max_k += 1
max_k += 1
next_parents_list = [[-1] * len(t_list) for _ in range(max_k + 1)]
next_parents_list[0] = next_parents
for k in range(1, max_k + 1):
for i in range(len(t_list)):
p = next_parents_list[k - 1][i]
if p != -1:
next_parents_list[k][i] = next_parents_list[k - 1][p]
# ダブリングによる回数計算
for a, b in ab:
a_t = T[a]
b_h = H[b]
a_t_index = t_map[a_t]
if t_next_map[a_t_index] >= b_h:
print(1)
continue
base_ans = 0
answer = MAX_INT
for k in reversed(range(max_k + 1)):
p = next_parents_list[k][a_t_index]
if p != -1:
if t_next_map[p] >= b_h:
answer = min(answer, base_ans + 1 + (1 << k))
else:
base_ans += (1 << k)
a_t_index = p
if answer == MAX_INT:
print(-1)
else:
print(answer)
if __name__ == "__main__":
main()