結果
問題 | No.694 square1001 and Permutation 3 |
ユーザー | vwxyz |
提出日時 | 2023-12-01 21:32:08 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,622 bytes |
コンパイル時間 | 196 ms |
コンパイル使用メモリ | 81,852 KB |
実行使用メモリ | 262,528 KB |
最終ジャッジ日時 | 2024-09-26 15:54:47 |
合計ジャッジ時間 | 3,712 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | MLE | - |
testcase_10 | WA | - |
testcase_11 | MLE | - |
testcase_12 | MLE | - |
testcase_13 | WA | - |
ソースコード
import sysreadline=sys.stdin.readline"""def Inversion_Number(lst,weight=False,weakly=False):compress,decompress=Compress(lst)compressed_lst=[compress[x] for x in lst]N=len(compress)if not weight:weight=[1]*len(lst)ST=Segment_Tree(N,lambda x,y:x+y,0)inversion_number=0for c,x in zip(weight,compressed_lst):inversion_number+=ST.Fold(x if weakly else x+1,N)*cST[x]+=creturn inversion_numberclass Segment_Tree:def __init__(self,N,f,e,lst=None,dynamic=False):self.f=fself.e=eself.N=Nif dynamic:self.segment_tree=defaultdict(lambda:self.e)else:if lst==None:self.segment_tree=[self.e]*2*self.Nelse:assert len(lst)<=self.Nself.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.stepif a==None:a=self.Nelse:a+=self.Nif b==None:b=self.N*2else:b+=self.Nreturn self.segment_tree[slice(a,b,c)]def __setitem__(self,i,x):if -self.N<=i<0:i+=self.N*2elif 0<=i<self.N:i+=self.Nelse:raise IndexError("list index out of range")self.segment_tree[i]=xwhile i>1:i>>= 1self.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]=xfor 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.Nelse:L+=self.Nif R==None:R=self.N*2else:R+=self.NvL=self.evR=self.ewhile L<R:if L&1:vL=self.f(vL,self.segment_tree[L])L+=1if R&1:R-=1vR=self.f(self.segment_tree[R],vR)L>>=1R>>=1return self.f(vL,vR)def Fold_Index(self,L=None,R=None):if L==None:L=self.Nelse:L+=self.Nif R==None:R=self.N*2else:R+=self.Nif L==R:return Nonex=self.Fold(L-self.N,R-self.N)while L<R:if L&1:if self.segment_tree[L]==x:i=LbreakL+=1if R&1:R-=1if self.segment_tree[R]==x:i=RbreakL>>=1R>>=1while i<self.N:if self.segment_tree[i]==self.segment_tree[i<<1]:i<<=1else:i<<=1i|=1i-=self.Nreturn idef Bisect_Right(self,L=None,f=None):if L==self.N:return self.Nif L==None:L=0L+=self.Nvl=self.evr=self.el,r=L,self.N*2while l<r:if l&1:vl=self.f(vl,self.segment_tree[l])l+=1if r&1:r-=1vr=self.f(self.segment_tree[r],vr)l>>=1r>>=1if f(self.f(vl,vr)):return self.Nv=self.ewhile True:while L%2==0:L>>=1vv=self.f(v,self.segment_tree[L])if f(vv):v=vvL+=1else:while L<self.N:L<<=1vv=self.f(v,self.segment_tree[L])if f(vv):v=vvL+=1return L-self.Ndef Bisect_Left(self,R=None,f=None):if R==0:return 0if R==None:R=self.NR+=self.Nvl=self.evr=self.el,r=self.N,Rwhile l<r:if l&1:vl=self.f(vl,self.segment_tree[l])l+=1if r&1:r-=1vr=self.f(self.segment_tree[r],vr)l>>=1r>>=1if f(self.f(vl,vr)):return 0v=self.ewhile True:R-=1while R>1 and R%2:R>>=1vv=self.f(self.segment_tree[R],v)if f(vv):v=vvelse:while R<self.N:R=2*R+1vv=self.f(self.segment_tree[R],v)if f(vv):v=vvR-=1return R+1-self.Ndef __str__(self):return "["+", ".join(map(str,self.segment_tree[self.N:]))+"]"def Swap_Count(N,A,B):if sorted(A)==sorted(B):idxA={tpl:i for i,tpl in enumerate(sorted([(A[i],i) for i in range(N)]))}idxB={tpl:i for i,tpl in enumerate(sorted([(B[i],i) for i in range(N)]))}for i in range(N):A[i]=idxA[(A[i],i)]B[i]=idxB[(B[i],i)]idx={A[i]:i for i in range(N)}for i in range(N):B[i]=idx[B[i]]retu=Inversion_Number(B)else:retu=-1return retudef Compress(lst):decomp=sorted(list(set(lst)))comp={x:i for i,x in enumerate(decomp)}return comp,decomp"""class Bit:def __init__(self, n):self.size = nself.tree = [0] * (n + 1)def sum(self, i):s = 0while i > 0:s += self.tree[i]i -= i & -ireturn sdef add(self, i, x):while i <= self.size:self.tree[i] += xi += i & -iN=int(readline())A=[int(readline()) for i in range(N)]comp={a:i for i,a in enumerate(sorted(set(A)),1)}le=len(comp)for i in range(N):A[i]=comp[A[i]]del compans=0bit=Bit(le+1)