結果
| 問題 |
No.1300 Sum of Inversions
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2020-11-27 22:05:28 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,367 bytes |
| コンパイル時間 | 420 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 178,384 KB |
| 最終ジャッジ日時 | 2024-07-26 12:53:23 |
| 合計ジャッジ時間 | 18,041 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 2 WA * 32 |
ソースコード
import sys
input = sys.stdin.readline
N=int(input())
A=list(map(int,input().split()))
mod=998244353
SA=[0]+sorted(set(A))
compression_dict={a: ind for ind, a in enumerate(SA)}
A=[compression_dict[a] for a in A]
LEN=2*10**5
BIT=[0]*(LEN+1) # 1-indexedなtree
def update(v,w): # index vにwを加える
while v<=LEN:
BIT[v]+=w
v+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4
def getvalue(v): # [1,v]の区間の和を求める
ANS=0
while v!=0:
ANS+=BIT[v]
v-=(v&(-v)) # 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
return ANS
BIT2=[0]*(LEN+1) # 1-indexedなtree
def update2(v,w): # index vにwを加える
while v<=LEN:
BIT2[v]+=w
v+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4
def getvalue2(v): # [1,v]の区間の和を求める
ANS=0
while v!=0:
ANS+=BIT2[v]
v-=(v&(-v)) # 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
return ANS
LEFT=[0]*N
LEFT2=[0]*N
for i in range(N):
LEFT[i]=getvalue(A[i])
LEFT2[i]=getvalue2(A[i])
update(1,SA[A[i]])
update(A[i],-SA[A[i]])
update2(1,1)
update2(A[i],-1)
#print(LEFT)
LEN=2*10**5
BIT=[0]*(LEN+1) # 1-indexedなtree
def update(v,w): # index vにwを加える
while v<=LEN:
BIT[v]+=w
v+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4
def getvalue(v): # [1,v]の区間の和を求める
ANS=0
while v!=0:
ANS+=BIT[v]
v-=(v&(-v)) # 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
return ANS
BIT2=[0]*(LEN+1) # 1-indexedなtree
def update2(v,w): # index vにwを加える
while v<=LEN:
BIT2[v]+=w
v+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4
def getvalue2(v): # [1,v]の区間の和を求める
ANS=0
while v!=0:
ANS+=BIT2[v]
v-=(v&(-v)) # 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
return ANS
RIGHT=[0]*N
RIGHT2=[0]*N
for i in range(N-1,-1,-1):
RIGHT[i]=getvalue(A[i])
RIGHT2[i]=getvalue2(A[i])
update(A[i]+1,SA[A[i]])
update2(A[i]+1,1)
#print(RIGHT)
ANS=0
for i in range(N):
ANS+=LEFT[i]*RIGHT2[i]+RIGHT[i]*LEFT2[i]+(LEFT2[i]*RIGHT2[i]*A[i])
ANS%=mod
print(ANS)
titia