結果
問題 | No.992 最長増加部分列の数え上げ |
ユーザー | vwxyz |
提出日時 | 2024-04-14 10:55:28 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,230 ms / 2,000 ms |
コード長 | 6,049 bytes |
コンパイル時間 | 264 ms |
コンパイル使用メモリ | 82,492 KB |
実行使用メモリ | 225,936 KB |
最終ジャッジ日時 | 2024-10-03 07:50:05 |
合計ジャッジ時間 | 33,757 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
55,040 KB |
testcase_01 | AC | 36 ms
55,520 KB |
testcase_02 | AC | 37 ms
55,424 KB |
testcase_03 | AC | 39 ms
56,224 KB |
testcase_04 | AC | 544 ms
129,372 KB |
testcase_05 | AC | 479 ms
116,292 KB |
testcase_06 | AC | 795 ms
140,040 KB |
testcase_07 | AC | 547 ms
122,156 KB |
testcase_08 | AC | 499 ms
101,436 KB |
testcase_09 | AC | 570 ms
123,424 KB |
testcase_10 | AC | 606 ms
140,948 KB |
testcase_11 | AC | 723 ms
158,744 KB |
testcase_12 | AC | 334 ms
94,420 KB |
testcase_13 | AC | 535 ms
121,552 KB |
testcase_14 | AC | 500 ms
121,492 KB |
testcase_15 | AC | 317 ms
95,276 KB |
testcase_16 | AC | 1,041 ms
206,708 KB |
testcase_17 | AC | 349 ms
102,324 KB |
testcase_18 | AC | 553 ms
121,420 KB |
testcase_19 | AC | 853 ms
155,628 KB |
testcase_20 | AC | 1,070 ms
225,320 KB |
testcase_21 | AC | 1,156 ms
224,404 KB |
testcase_22 | AC | 1,141 ms
225,772 KB |
testcase_23 | AC | 1,130 ms
225,576 KB |
testcase_24 | AC | 1,164 ms
225,636 KB |
testcase_25 | AC | 1,111 ms
225,308 KB |
testcase_26 | AC | 1,230 ms
225,936 KB |
testcase_27 | AC | 1,108 ms
225,364 KB |
testcase_28 | AC | 1,099 ms
224,692 KB |
testcase_29 | AC | 1,140 ms
225,512 KB |
testcase_30 | AC | 727 ms
215,860 KB |
testcase_31 | AC | 715 ms
217,704 KB |
testcase_32 | AC | 725 ms
214,980 KB |
testcase_33 | AC | 719 ms
215,288 KB |
testcase_34 | AC | 744 ms
220,460 KB |
testcase_35 | AC | 710 ms
218,784 KB |
testcase_36 | AC | 698 ms
215,068 KB |
testcase_37 | AC | 707 ms
221,208 KB |
testcase_38 | AC | 687 ms
215,832 KB |
testcase_39 | AC | 710 ms
218,012 KB |
testcase_40 | AC | 771 ms
215,972 KB |
testcase_41 | AC | 815 ms
216,436 KB |
testcase_42 | AC | 766 ms
219,272 KB |
testcase_43 | AC | 753 ms
219,284 KB |
testcase_44 | AC | 771 ms
217,436 KB |
ソースコード
from collections import defaultdict 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[i] for i in range(self.N,self.N*2)]))+"]" def Compress(lst): decomp=sorted(list(set(lst))) comp={x:i for i,x in enumerate(decomp)} return comp,decomp N=int(input()) A=list(map(int,input().split())) inf=1<<30 comp,decomp=Compress(A+[-inf]) le=len(comp) for i in range(N): A[i]=comp[A[i]] mod=10**9+7 ST_max=Segment_Tree(le,max,-inf) ST_max[0]=0 dp=[] for a in A: ma=ST_max.Fold(0,a)+1 dp.append(ma) ST_max[a]=ma AA=[[] for i in range(le+1)] AA[0].append(0) for i in range(N): AA[dp[i]].append(A[i]) comp,decomp=[],[] for i in range(le+1): c,d=Compress(AA[i]) comp.append(c) decomp.append(d) ST_sum=[Segment_Tree(len(comp[i]),lambda x,y:(x+y)%mod,0) for i in range(le+1)] ST_sum[0][0]=1 for i,a in enumerate(A): bisect.bisect_left(decomp[dp[i]-1],a) ST_sum[dp[i]][comp[dp[i]][a]]+=ST_sum[dp[i]-1].Fold(0,bisect.bisect_left(decomp[dp[i]-1],a)) ST_sum[dp[i]][comp[dp[i]][a]]%=mod ans=ST_sum[max(dp)].Fold() print(ans)