結果

問題 No.1300 Sum of Inversions
ユーザー tcltktcltk
提出日時 2021-02-02 02:34:38
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,062 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 86,960 KB
実行使用メモリ 202,744 KB
最終ジャッジ日時 2023-09-12 10:57:47
合計ジャッジ時間 28,405 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 214 ms
80,600 KB
testcase_01 AC 215 ms
80,716 KB
testcase_02 AC 214 ms
80,692 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 513 ms
136,780 KB
testcase_34 AC 520 ms
141,352 KB
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#region Header
#!/usr/bin/env python3
# from typing import *

import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq

def input():
    return sys.stdin.readline()[:-1]

sys.setrecursionlimit(1000000)
#endregion

# _INPUT = """10
# 3 1 4 1 5 9 2 6 5 3
# """
# sys.stdin = io.StringIO(_INPUT)

MOD = 998244353

class BIT:
    """
    Binary Indexed Tree (Fenwick Tree), 1-indexed
    """

    def __init__(self, n):
        """
        Parameters
        ----------
        n : int
            要素数。index は 0..n になる。
        """
        self.size = n
        self.data = [0] * (n+1)
        # self.depth = n.bit_length()

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

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

    def get_rsum(self, l, r):
        """
        [l, r) の sum
        """
        return self.get_sum(r) - self.get_sum(l-1)

def main():
    N = int(input())
    A = list(map(int, input().split()))

    T = {a: i for i, a in enumerate(sorted(set(A)))}

    # 真ん中の A_j を固定

    # A_i > A_j となる A_i
    N1 = [0] * N
    L1 = [0] * N
    bit11 = BIT(N)
    bit12 = BIT(N)
    for i, a in reversed(list(enumerate(A))):
        N1[i] = bit11.get_sum(T[a]-1+1) % MOD
        L1[i] = bit12.get_sum(T[a]-1+1) % MOD
        bit11.add(T[a]+1, 1)
        bit12.add(T[a]+1, a)

    # A_j > A_k となる A_k
    N2 = [0] * N
    L2 = [0] * N
    bit21 = BIT(N)
    bit22 = BIT(N)
    for i, a in enumerate(A):
        N2[i] = bit21.get_rsum(T[a]+1+1, N) % MOD
        L2[i] = bit22.get_rsum(T[a]+1+1, N) % MOD
        bit21.add(T[a]+1, 1)
        bit22.add(T[a]+1, a)

    ans = sum((N1[i] * L2[i] + N2[i] * L1[i] + (N1[i] * N2[i]) * A[i]) % MOD for i in range(N))
    print(ans)

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