結果

問題 No.1300 Sum of Inversions
ユーザー titiatitia
提出日時 2020-11-27 22:05:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,367 bytes
コンパイル時間 464 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 56,712 KB
最終ジャッジ日時 2023-10-01 06:00:53
合計ジャッジ時間 4,721 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
17,432 KB
testcase_01 AC 27 ms
12,980 KB
testcase_02 AC 27 ms
12,908 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

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