結果

問題 No.1300 Sum of Inversions
ユーザー titiatitia
提出日時 2020-11-27 22:05:28
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,367 bytes
コンパイル時間 903 ms
コンパイル使用メモリ 86,840 KB
実行使用メモリ 179,512 KB
最終ジャッジ日時 2023-10-01 06:02:22
合計ジャッジ時間 20,505 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
77,632 KB
testcase_01 AC 80 ms
77,460 KB
testcase_02 AC 79 ms
77,660 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
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 -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 392 ms
112,044 KB
testcase_34 AC 402 ms
110,680 KB
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0