結果

問題 No.1300 Sum of Inversions
ユーザー neterukunneterukun
提出日時 2020-11-27 22:04:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,108 ms / 2,000 ms
コード長 1,998 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 86,720 KB
実行使用メモリ 125,412 KB
最終ジャッジ日時 2023-10-01 06:00:48
合計ジャッジ時間 28,439 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,228 KB
testcase_01 AC 75 ms
70,984 KB
testcase_02 AC 74 ms
71,440 KB
testcase_03 AC 898 ms
103,152 KB
testcase_04 AC 830 ms
102,936 KB
testcase_05 AC 691 ms
99,456 KB
testcase_06 AC 932 ms
108,436 KB
testcase_07 AC 923 ms
105,364 KB
testcase_08 AC 991 ms
110,192 KB
testcase_09 AC 984 ms
109,404 KB
testcase_10 AC 574 ms
97,060 KB
testcase_11 AC 589 ms
96,960 KB
testcase_12 AC 834 ms
102,932 KB
testcase_13 AC 821 ms
103,144 KB
testcase_14 AC 1,108 ms
111,084 KB
testcase_15 AC 1,004 ms
109,248 KB
testcase_16 AC 879 ms
102,964 KB
testcase_17 AC 550 ms
95,008 KB
testcase_18 AC 627 ms
98,116 KB
testcase_19 AC 723 ms
101,044 KB
testcase_20 AC 751 ms
101,308 KB
testcase_21 AC 750 ms
100,952 KB
testcase_22 AC 686 ms
99,368 KB
testcase_23 AC 922 ms
108,456 KB
testcase_24 AC 700 ms
98,980 KB
testcase_25 AC 612 ms
97,576 KB
testcase_26 AC 602 ms
97,448 KB
testcase_27 AC 674 ms
99,380 KB
testcase_28 AC 1,013 ms
110,356 KB
testcase_29 AC 747 ms
101,112 KB
testcase_30 AC 990 ms
109,716 KB
testcase_31 AC 694 ms
98,852 KB
testcase_32 AC 709 ms
99,048 KB
testcase_33 AC 415 ms
114,280 KB
testcase_34 AC 437 ms
125,412 KB
testcase_35 AC 422 ms
109,512 KB
testcase_36 AC 496 ms
112,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BinaryIndexedTree:
    """一点加算、区間総和クエリをそれぞれO(logN)で答えるデータ構造。"""
    def __init__(self, n):
        self.size = n
        self.bit = [0] * (n + 1)

    def build(self, array):
        """arrayを初期値とするBinaryIndexedTreeを構築する。O(N)"""
        for i, val in enumerate(array):
            self.bit[i + 1] = val
        for i in range(1, self.size):
            if i + (i & -i) > self.size:
                continue
            self.bit[i + (i & -i)] += self.bit[i]

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

    def add(self, i, val):
        """i番目の要素にvalを加算する。O(logN)"""
        i += 1
        while i <= self.size:
            self.bit[i] += val
            i += i & -i

    def sum(self, l, r):
        """区間[l, r)の和を求める。O(logN)"""
        return self._sum(r) - self._sum(l)


n = int(input())
a = list(map(int, input().split()))
MOD = 998244353


sorted_a = sorted([(a[i], i) for i in range(n)]) + [(-1, -1)]

bitl_cnt = BinaryIndexedTree(n)
bitl_val = BinaryIndexedTree(n)
bitl_cnt.build([1] * n)
bitl_val.build([val for val in a])

bits_cnt = BinaryIndexedTree(n)
bits_val = BinaryIndexedTree(n)

ans = 0

i = 0
while i < n:
    val, idx = sorted_a[i]
    idxs = [idx]
    while sorted_a[i][0] == sorted_a[i + 1][0]:
        i += 1
        idxs.append(sorted_a[i][1])
    for idx in idxs:
        bitl_cnt.add(idx, -1)
        bitl_val.add(idx, -val)
    
    for idx in idxs:
        cnt_large = bitl_cnt.sum(0, idx)
        sum_large = bitl_val.sum(0, idx)
        cnt_small = bits_cnt.sum(idx + 1, n)
        sum_small = bits_val.sum(idx + 1, n)
        ans += val * (cnt_large) * (cnt_small)
        ans += cnt_large * sum_small + cnt_small * sum_large
        ans %= MOD
    
    for idx in idxs:
        bits_cnt.add(idx, 1)
        bits_val.add(idx, val)
    i += 1

print(ans)
0