結果

問題 No.1300 Sum of Inversions
ユーザー titia
提出日時 2020-11-27 22:08:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 545 ms / 2,000 ms
コード長 2,371 bytes
コンパイル時間 451 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 178,336 KB
最終ジャッジ日時 2024-07-26 13:00:22
合計ジャッジ時間 15,412 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

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