結果
問題 | No.2242 Cities and Teleporters |
ユーザー | vwxyz |
提出日時 | 2023-03-31 10:01:22 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 7,749 bytes |
コンパイル時間 | 697 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 228,904 KB |
最終ジャッジ日時 | 2024-09-22 15:28:01 |
合計ジャッジ時間 | 24,942 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
53,888 KB |
testcase_01 | AC | 44 ms
54,016 KB |
testcase_02 | AC | 45 ms
54,272 KB |
testcase_03 | AC | 45 ms
54,400 KB |
testcase_04 | AC | 44 ms
54,144 KB |
testcase_05 | WA | - |
testcase_06 | AC | 1,163 ms
222,224 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
ソースコード
import sys readline=sys.stdin.readline import bisect class Segment_Tree: def __init__(self,N,f,e,lst=None,dynamic=False): self.f=f self.e=e self.N=N if dynamic: self.segment_tree=defaultdict(lambda:self.e) else: if lst==None: self.segment_tree=[self.e]*2*self.N else: assert len(lst)<=self.N self.segment_tree=[self.e]*self.N+[x for x in lst]+[self.e]*(N-len(lst)) for i in range(self.N-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def __getitem__(self,i): if type(i)==int: if -self.N<=i<0: return self.segment_tree[i+self.N*2] elif 0<=i<self.N: return self.segment_tree[i+self.N] else: raise IndexError("list index out of range") else: a,b,c=i.start,i.stop,i.step if a==None: a=self.N else: a+=self.N if b==None: b=self.N*2 else: b+=self.N return self.segment_tree[slice(a,b,c)] def __setitem__(self,i,x): if -self.N<=i<0: i+=self.N*2 elif 0<=i<self.N: i+=self.N else: raise IndexError("list index out of range") self.segment_tree[i]=x while i>1: i>>= 1 self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Build(self,lst): for i,x in enumerate(lst,self.N): self.segment_tree[i]=x for i in range(self.N-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Fold(self,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=self.N*2 else: R+=self.N vL=self.e vR=self.e while L<R: if L&1: vL=self.f(vL,self.segment_tree[L]) L+=1 if R&1: R-=1 vR=self.f(self.segment_tree[R],vR) L>>=1 R>>=1 return self.f(vL,vR) def Fold_Index(self,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=self.N*2 else: R+=self.N if L==R: return None x=self.Fold(L-self.N,R-self.N) while L<R: if L&1: if self.segment_tree[L]==x: i=L break L+=1 if R&1: R-=1 if self.segment_tree[R]==x: i=R break L>>=1 R>>=1 while i<self.N: if self.segment_tree[i]==self.segment_tree[i<<1]: i<<=1 else: i<<=1 i|=1 i-=self.N return i def Bisect_Right(self,L=None,f=None): if L==self.N: return self.N if L==None: L=0 L+=self.N vl=self.e vr=self.e l,r=L,self.N*2 while l<r: if l&1: vl=self.f(vl,self.segment_tree[l]) l+=1 if r&1: r-=1 vr=self.f(self.segment_tree[r],vr) l>>=1 r>>=1 if f(self.f(vl,vr)): return self.N v=self.e while True: while L%2==0: L>>=1 vv=self.f(v,self.segment_tree[L]) if f(vv): v=vv L+=1 else: while L<self.N: L<<=1 vv=self.f(v,self.segment_tree[L]) if f(vv): v=vv L+=1 return L-self.N def Bisect_Left(self,R=None,f=None): if R==0: return 0 if R==None: R=self.N R+=self.N vl=self.e vr=self.e l,r=self.N,R while l<r: if l&1: vl=self.f(vl,self.segment_tree[l]) l+=1 if r&1: r-=1 vr=self.f(self.segment_tree[r],vr) l>>=1 r>>=1 if f(self.f(vl,vr)): return 0 v=self.e while True: R-=1 while R>1 and R%2: R>>=1 vv=self.f(self.segment_tree[R],v) if f(vv): v=vv else: while R<self.N: R=2*R+1 vv=self.f(self.segment_tree[R],v) if f(vv): v=vv R-=1 return R+1-self.N def __str__(self): return "["+", ".join(map(str,self.segment_tree[self.N:]))+"]" class Path_Doubling: def __init__(self,N,permutation,lst=None,f=None,e=None): self.N=N self.permutation=permutation self.lst=lst self.f=f self.e=e def Build_Next(self,K=None): if K==None: self.K=self.N else: self.K=K self.k=self.K.bit_length() self.permutation_doubling=[[self.permutation[n]] for n in range(self.N)] if self.lst!=None: self.doubling=[[self.lst[n]] for n in range(self.N)] for k in range(1,self.k): for n in range(self.N): if self.permutation_doubling[n][k-1]==None: self.permutation_doubling[n].append(None) if self.f!=None: self.doubling[n].append(None) if self.permutation_doubling[n][k-1]!=None: self.permutation_doubling[n].append(self.permutation_doubling[self.permutation_doubling[n][k-1]][k-1]) if self.f!=None: self.doubling[n].append(self.f(self.doubling[n][k-1],self.doubling[self.permutation_doubling[n][k-1]][k-1])) def Permutation_Doubling(self,N,K): if K<0 or 1<<self.k<=K: return None for k in range(self.k): if K>>k&1 and N!=None: N=self.permutation_doubling[N][k] return N def Doubling(self,N,K): if K<0: return self.e retu=self.e for k in range(self.k): if K>>k&1: if self.permutation_doubling[N][k]==None: return None retu=self.f(retu,self.doubling[N][k]) N=self.permutation_doubling[N][k] return retu def Bisect(self,x,is_ok): if not is_ok(x): return -1,None K=0 for k in range(self.k-1,-1,-1): if K|1<<k<=self.K and is_ok(self.permutation_doubling[x][k]): K|=1<<k x=self.permutation_doubling[x][k] return K,x N=int(readline()) H=list(map(int,readline().split())) T=list(map(int,readline().split())) idx=sorted([i for i in range(N)],key=lambda i:H[i]) inf=1<<30 ST=Segment_Tree(N,max,-inf,[T[i] for i in idx]) perm=[None]*N for i in range(N): j=ST.Fold_Index(0,bisect.bisect_right(H,T[i])) if j==None or T[i]>T[idx[j]]: j=i else: j=idx[j] perm[i]=j PD=Path_Doubling(N,perm) PD.Build_Next() Q=int(readline()) for q in range(Q): A,B=map(int,readline().split()) A-=1;B-=1 ans,_=PD.Bisect(A,lambda x:T[x]<H[B]) if ans==N: ans=-1 else: ans+=2 print(ans)