結果

問題 No.1300 Sum of Inversions
ユーザー ntudantuda
提出日時 2021-10-16 11:22:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 860 ms / 2,000 ms
コード長 1,356 bytes
コンパイル時間 989 ms
コンパイル使用メモリ 81,588 KB
実行使用メモリ 187,796 KB
最終ジャッジ日時 2023-10-17 22:07:01
合計ジャッジ時間 24,292 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,368 KB
testcase_01 AC 39 ms
53,368 KB
testcase_02 AC 38 ms
53,368 KB
testcase_03 AC 694 ms
126,940 KB
testcase_04 AC 658 ms
126,544 KB
testcase_05 AC 548 ms
120,004 KB
testcase_06 AC 767 ms
152,276 KB
testcase_07 AC 726 ms
131,716 KB
testcase_08 AC 789 ms
157,576 KB
testcase_09 AC 792 ms
157,312 KB
testcase_10 AC 476 ms
119,420 KB
testcase_11 AC 474 ms
119,456 KB
testcase_12 AC 655 ms
126,580 KB
testcase_13 AC 667 ms
125,944 KB
testcase_14 AC 860 ms
187,796 KB
testcase_15 AC 789 ms
157,612 KB
testcase_16 AC 696 ms
127,104 KB
testcase_17 AC 460 ms
114,732 KB
testcase_18 AC 523 ms
119,224 KB
testcase_19 AC 604 ms
121,428 KB
testcase_20 AC 607 ms
121,776 KB
testcase_21 AC 609 ms
122,028 KB
testcase_22 AC 565 ms
120,268 KB
testcase_23 AC 736 ms
131,648 KB
testcase_24 AC 576 ms
118,792 KB
testcase_25 AC 500 ms
119,904 KB
testcase_26 AC 498 ms
119,856 KB
testcase_27 AC 537 ms
121,480 KB
testcase_28 AC 824 ms
165,532 KB
testcase_29 AC 607 ms
121,992 KB
testcase_30 AC 774 ms
157,856 KB
testcase_31 AC 543 ms
120,040 KB
testcase_32 AC 590 ms
119,368 KB
testcase_33 AC 300 ms
102,928 KB
testcase_34 AC 328 ms
99,792 KB
testcase_35 AC 514 ms
140,352 KB
testcase_36 AC 542 ms
186,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Bit1:
    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

class Bit2:
    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]
            i -= i & -i
        return s

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

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

bit11 = Bit1(M)
bit21 = Bit1(M)
bit12 = Bit2(M)
bit22 = Bit2(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