結果

問題 No.1300 Sum of Inversions
ユーザー ntudantuda
提出日時 2021-10-16 11:15:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,015 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 81,848 KB
実行使用メモリ 188,076 KB
最終ジャッジ日時 2023-10-17 22:06:17
合計ジャッジ時間 24,509 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,328 KB
testcase_01 AC 40 ms
53,328 KB
testcase_02 AC 36 ms
53,328 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 259 ms
102,884 KB
testcase_34 AC 284 ms
99,688 KB
testcase_35 AC 490 ms
140,572 KB
testcase_36 AC 489 ms
186,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
A = list(map(int,input().split()))
mod = 998244353

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            s %= mod
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            self.tree[i] %= mod
            i += i & -i

B = list(set(A))
B.sort()
M = len(B)
D = dict(zip(B,range(M)))

bit11 = Bit(M)
bit21 = Bit(M)
bit12 = Bit(M)
bit22 = Bit(M)
ans = 0
for i in range(N):
    tmp = bit21.sum(M) - bit21.sum(D[A[i]]+1)
    cnt = bit22.sum(M) - bit22.sum(D[A[i]]+1)
    if cnt > 0:
        ans = (ans + A[i] * cnt + tmp) % mod
    tmp = bit11.sum(M) - bit11.sum(D[A[i]]+1)
    cnt = bit12.sum(M) - bit12.sum(D[A[i]]+1)
    if cnt > 0:
        bit21.add(D[A[i]]+1,tmp + A[i] * cnt)
        bit22.add(D[A[i]]+1,cnt)
    bit11.add(D[A[i]]+1,A[i])
    bit12.add(D[A[i]]+1,1)

print(ans)
0