結果
問題 |
No.2242 Cities and Teleporters
|
ユーザー |
|
提出日時 | 2025-01-14 00:38:19 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,833 bytes |
コンパイル時間 | 709 ms |
コンパイル使用メモリ | 82,108 KB |
実行使用メモリ | 534,076 KB |
最終ジャッジ日時 | 2025-01-14 00:39:30 |
合計ジャッジ時間 | 52,943 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 TLE * 5 MLE * 1 |
ソースコード
## https://yukicoder.me/problems/no/2242 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 = {} 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] = {"reachable": h0, "next_t":-1} else: t_next_map[t] = {"reachable": h0, "next_t":ct} else: t_next_map[t] = {"reachable": -1, "next_t":-1} # ダブリングの準備のため next_parents = [-1] * len(t_list) for i in range(len(t_list)): xx = t_next_map[t_list[i]]["next_t"] if xx != -1: j = t_map[xx] next_parents[i] = j 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] if t_next_map[a_t]["reachable"] >= b_h: print(1) continue a_t_index = t_map[a_t] base_ans = 0 answer = 10 ** 18 for k in reversed(range(max_k + 1)): p = next_parents_list[k][a_t_index] if p != -1: if t_next_map[t_list[p]]["reachable"] >= b_h: answer = min(answer, base_ans + 1 + (1 << k)) else: base_ans += (1 << k) a_t_index = p if answer == 10 ** 18: print(-1) else: print(answer) if __name__ == "__main__": main()