結果

問題 No.1300 Sum of Inversions
ユーザー titiatitia
提出日時 2020-11-27 22:08:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 730 ms / 2,000 ms
コード長 2,371 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 179,352 KB
最終ジャッジ日時 2023-10-01 06:10:20
合計ジャッジ時間 20,893 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
77,528 KB
testcase_01 AC 77 ms
77,440 KB
testcase_02 AC 78 ms
77,440 KB
testcase_03 AC 573 ms
138,848 KB
testcase_04 AC 565 ms
138,524 KB
testcase_05 AC 471 ms
116,440 KB
testcase_06 AC 644 ms
164,560 KB
testcase_07 AC 630 ms
163,356 KB
testcase_08 AC 688 ms
169,660 KB
testcase_09 AC 686 ms
169,644 KB
testcase_10 AC 393 ms
120,976 KB
testcase_11 AC 386 ms
120,736 KB
testcase_12 AC 547 ms
138,152 KB
testcase_13 AC 551 ms
136,964 KB
testcase_14 AC 730 ms
179,352 KB
testcase_15 AC 664 ms
169,492 KB
testcase_16 AC 586 ms
139,088 KB
testcase_17 AC 384 ms
119,840 KB
testcase_18 AC 426 ms
112,920 KB
testcase_19 AC 501 ms
121,332 KB
testcase_20 AC 513 ms
121,332 KB
testcase_21 AC 498 ms
121,592 KB
testcase_22 AC 467 ms
116,552 KB
testcase_23 AC 622 ms
164,588 KB
testcase_24 AC 477 ms
116,428 KB
testcase_25 AC 416 ms
114,580 KB
testcase_26 AC 398 ms
115,464 KB
testcase_27 AC 454 ms
116,320 KB
testcase_28 AC 696 ms
178,124 KB
testcase_29 AC 495 ms
121,100 KB
testcase_30 AC 662 ms
170,020 KB
testcase_31 AC 459 ms
116,376 KB
testcase_32 AC 476 ms
116,960 KB
testcase_33 AC 393 ms
111,840 KB
testcase_34 AC 413 ms
110,436 KB
testcase_35 AC 463 ms
141,052 KB
testcase_36 AC 521 ms
178,384 KB
権限があれば一括ダウンロードができます

ソースコード

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]*SA[A[i]])
    ANS%=mod

print(ANS)
0