結果
問題 | No.2242 Cities and Teleporters |
ユーザー |
|
提出日時 | 2023-03-10 23:05:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,088 ms / 3,000 ms |
コード長 | 1,930 bytes |
コンパイル時間 | 307 ms |
コンパイル使用メモリ | 82,268 KB |
実行使用メモリ | 162,976 KB |
最終ジャッジ日時 | 2024-09-18 05:15:24 |
合計ジャッジ時間 | 19,798 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
import sys, randominput = lambda : sys.stdin.readline().rstrip()write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x))debug = lambda x: sys.stderr.write(x+"\n")YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO); INF=10**18LI = lambda : list(map(int, input().split())); II=lambda : int(input()); SI=lambda : [ord(c)-ord("a") for c in input()]def debug(_l_):for s in _l_.split():print(f"{s}={eval(s)}", end=" ")print()def dlist(*l, fill=0):if len(l)==1:return [fill]*l[0]ll = l[1:]return [dlist(*ll, fill=fill) for _ in range(l[0])]n = int(input())h = list(map(int, input().split()))t = LI()index = list(range(n))index.sort(key=lambda i: h[i])pos = [0]*nfor i in range(n):pos[index[i]] = ih = [h[ind] for ind in index]t = [t[ind] for ind in index]vs = [0]*nns = [-1]*nfrom bisect import bisect_right as brfor i in range(n):v = br(h, t[i])vs[i] = vns[i] = v-1cum = ns[:]cum[0] = max(cum[0], 0)for i in range(1,n):cum[i] = max(i, cum[i], cum[i-1])dns = [cum]for i in range(30):p = dns[-1]l = [-1]*nfor j in range(n):if p[j]==-1:l[j] = -1else:l[j] = max(p[j], p[p[j]])dns.append(l)q = II()for _ in range(q):a,b = LI()a -= 1b -= 1# print(a,b,pos[a],pos[b])a = pos[a]b = pos[b]res = 1cur = ns[a]if cur==-1 or dns[-1][cur]==-1 or dns[-1][cur]<b:res = -1elif cur>=b:res = 1else:for i in range(30)[::-1]:if dns[i][cur]>=b:continueelif dns[i][cur]!=-1:res += (1<<i)cur = dns[i][cur]else:res = -1breakelse:if dns[0][cur]>=b:res += 1else:res = -1print(res)