結果

問題 No.1300 Sum of Inversions
ユーザー roarisroaris
提出日時 2020-11-28 20:12:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 747 ms / 2,000 ms
コード長 1,269 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 182,864 KB
最終ジャッジ日時 2023-10-10 01:06:58
合計ジャッジ時間 21,432 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,500 KB
testcase_01 AC 87 ms
71,448 KB
testcase_02 AC 86 ms
71,696 KB
testcase_03 AC 674 ms
158,172 KB
testcase_04 AC 607 ms
157,740 KB
testcase_05 AC 527 ms
140,888 KB
testcase_06 AC 686 ms
168,472 KB
testcase_07 AC 660 ms
167,040 KB
testcase_08 AC 710 ms
173,548 KB
testcase_09 AC 706 ms
173,016 KB
testcase_10 AC 425 ms
134,856 KB
testcase_11 AC 424 ms
133,992 KB
testcase_12 AC 578 ms
157,688 KB
testcase_13 AC 577 ms
156,896 KB
testcase_14 AC 747 ms
168,668 KB
testcase_15 AC 716 ms
173,176 KB
testcase_16 AC 614 ms
158,680 KB
testcase_17 AC 425 ms
129,224 KB
testcase_18 AC 479 ms
134,396 KB
testcase_19 AC 543 ms
148,028 KB
testcase_20 AC 555 ms
148,972 KB
testcase_21 AC 562 ms
148,944 KB
testcase_22 AC 528 ms
140,888 KB
testcase_23 AC 694 ms
168,568 KB
testcase_24 AC 488 ms
141,080 KB
testcase_25 AC 463 ms
133,888 KB
testcase_26 AC 431 ms
133,236 KB
testcase_27 AC 486 ms
140,628 KB
testcase_28 AC 719 ms
182,864 KB
testcase_29 AC 553 ms
148,240 KB
testcase_30 AC 694 ms
173,428 KB
testcase_31 AC 527 ms
141,012 KB
testcase_32 AC 583 ms
141,584 KB
testcase_33 AC 289 ms
108,596 KB
testcase_34 AC 296 ms
111,764 KB
testcase_35 AC 418 ms
182,060 KB
testcase_36 AC 464 ms
172,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

def compress(l):
    l = list(set(l))
    l.sort()
    idx = defaultdict(int)

    for i in range(len(l)):
        idx[l[i]] = i
    
    return idx

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0]*(n+1)
    
    def add(self, i, x):
        i += 1
        
        while i<=self.n:
            self.bit[i] += x
            i += i&(-i)

    def acc(self, i):
        s = 0
        
        while i>0:
            s += self.bit[i]
            i -= i&(-i)
        
        return s

N = int(input())
A = list(map(int, input().split()))
idx = compress(A)
n = len(idx.keys())
bit1 = BIT(n)
bit2 = BIT(n)
upper1 = [0]*N
upper2 = [0]*N

for i in range(N):
    upper1[i] = bit1.acc(n)-bit1.acc(idx[A[i]]+1)
    upper2[i] = bit2.acc(n)-bit2.acc(idx[A[i]]+1)
    bit1.add(idx[A[i]], 1)
    bit2.add(idx[A[i]], A[i])

bit1 = BIT(n)
bit2 = BIT(n)
lower1 = [0]*N
lower2 = [0]*N

for i in range(N-1, -1, -1):
    lower1[i] = bit1.acc(idx[A[i]])
    lower2[i] = bit2.acc(idx[A[i]])
    bit1.add(idx[A[i]], 1)
    bit2.add(idx[A[i]], A[i])

ans = 0
MOD = 998244353

for i in range(N):
    ans += A[i]*upper1[i]*lower1[i]+upper1[i]*lower2[i]+upper2[i]*lower1[i]
    ans %= MOD

print(ans)
0