結果

問題 No.1300 Sum of Inversions
ユーザー donutholedonuthole
提出日時 2020-11-28 16:51:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,653 bytes
コンパイル時間 528 ms
コンパイル使用メモリ 10,956 KB
実行使用メモリ 54,768 KB
最終ジャッジ日時 2023-10-10 00:43:27
合計ジャッジ時間 4,731 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
14,956 KB
testcase_01 AC 38 ms
10,572 KB
testcase_02 AC 37 ms
10,616 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing
import sys
import math
import collections
import bisect
import itertools
import heapq
import decimal
import copy

# sys.setrecursionlimit(10000001)
INF = 10 ** 20
# MOD = 10 ** 9 + 7
MOD = 998244353


def ni(): return int(sys.stdin.buffer.readline())
def ns(): return map(int, sys.stdin.buffer.readline().split())
def na(): return list(map(int, sys.stdin.buffer.readline().split()))
def na1(): return list(map(lambda x: int(x)-1, sys.stdin.buffer.readline().split()))


# ===CODE===
# https://ikatakos.com/pot/programming_algorithm/dynamic_programming/inversion
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]
            i -= i & -i
        return s

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


def main():
    n = ni()
    a = na()

    sa = sorted(list(set(a)))
    d = {ai: i+1 for i, ai in enumerate(sa)}

    cnt = Bit(n)
    val = Bit(n)

    cntRes = [0]*n
    valRes = [0]*n
    for i, ai in enumerate(a):
        idx = d[ai]
        cntRes[i] = cnt.sum(n)-cnt.sum(idx)
        valRes[i] = val.sum(n)-val.sum(idx)
        cnt.add(idx, 1)
        val.add(idx, ai)

    ans = 0
    cnt = Bit(n)
    val = Bit(n)
    for i in range(n-1, -1, -1):
        idx = d[a[i]]
        c = cnt.sum(idx-1)
        v = val.sum(idx-1)
        cnt.add(idx,1)
        val.add(idx,a[i])

        ans += a[i]*cntRes[i]*c
        ans += c*valRes[i]
        ans += v*cntRes[i]
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()
0