結果
問題 | No.2950 Max Min Product |
ユーザー |
![]() |
提出日時 | 2024-10-26 00:29:58 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,151 bytes |
コンパイル時間 | 450 ms |
コンパイル使用メモリ | 82,600 KB |
実行使用メモリ | 87,324 KB |
最終ジャッジ日時 | 2024-10-26 00:30:04 |
合計ジャッジ時間 | 6,128 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | TLE * 1 -- * 36 |
ソースコード
n=int(input()) a=list(map(int,input().split())) M=998244353 q=[(a[i],i) for i in range(n)] q.sort() p=[[] for i in range(n+1)] tl=1<<20 def update(p,x): while p<tl: st[p]+=x p+=p&(-p) return def getsum(p): a=0 while p>0: a+=st[p] p-=p&(-p) return a st=[0]*tl for v,i in q[::-1]: update(i+1,1) ok=i ng=-1 while abs(ok-ng)>1: m=(ok+ng)//2 l=m r=i if getsum(r+1)-getsum(l)==1: ok=m else: ng=m L=ok ok=i ng=n while abs(ok-ng)>1: m=(ok+ng)//2 l=i r=m if getsum(r+1)-getsum(l)==1: ok=m else: ng=m R=ok p[i]+=[(L,i,v)] p[R+1]+=[(L,i,pow(v,M-2,M))] st=[0]*tl for v,i in q: update(i+1,1) ok=i ng=-1 while abs(ok-ng)>1: m=(ok+ng)//2 l=m r=i if getsum(r+1)-getsum(l)==1: ok=m else: ng=m L=ok ok=i ng=n while abs(ok-ng)>1: m=(ok+ng)//2 l=i r=m if getsum(r+1)-getsum(l)==1: ok=m else: ng=m R=ok p[i]+=[(L,i,v)] p[R+1]+=[(L,i,pow(v,M-2,M))] ans=0 B=448 st1=[1]*B*B st2=[B]*B lt=[1]*B for y in range(n): for l,r,v in p[y]: yl=l//B yr=r//B for i in range(yl*B,yl*B+B): st1[i]*=lt[yl] st1[i]%=M lt[yl]=1 for i in range(yr*B,yr*B+B): st1[i]*=lt[yr] st1[i]%=M lt[yr]=1 if yl==yr: for i in range(l,r+1): st2[yl]-=st1[i] st1[i]*=v st1[i]%=M st2[yl]+=st1[i] st2[yl]%=M else: for i in range(l,yl*B+B): st2[yl]-=st1[i] st1[i]*=v st1[i]%=M st2[yl]+=st1[i] st2[yl]%=M for i in range(yl+1,yr): st2[i]*=v st2[i]%=M lt[i]=v for i in range(yr*B,r+1): st2[yr]-=st1[i] st1[i]*=v st1[i]%=M st2[yr]+=st1[i] st2[yr]%=M g=0 l=0 r=y yl=l//B yr=r//B for i in range(yl*B,yl*B+B): st1[i]*=lt[yl] st1[i]%=M lt[yl]=1 for i in range(yr*B,yr*B+B): st1[i]*=lt[yr] st1[i]%=M lt[yr]=1 if yl==yr: g+=sum(st1[l:r+1]) else: g+=sum(st1[l:yl*B+B]) g+=sum(st2[yl+1:yr]) g+=sum(st1[yr*B:r+1]) ans+=g ans%=M print(ans)