結果

問題 No.1300 Sum of Inversions
ユーザー HAPPAHAPPA
提出日時 2020-11-28 11:35:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 904 ms / 2,000 ms
コード長 2,513 bytes
コンパイル時間 428 ms
コンパイル使用メモリ 87,104 KB
実行使用メモリ 162,812 KB
最終ジャッジ日時 2023-10-09 23:48:43
合計ジャッジ時間 24,724 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,044 KB
testcase_01 AC 74 ms
71,308 KB
testcase_02 AC 75 ms
71,152 KB
testcase_03 AC 720 ms
132,440 KB
testcase_04 AC 710 ms
132,352 KB
testcase_05 AC 587 ms
119,096 KB
testcase_06 AC 812 ms
153,864 KB
testcase_07 AC 771 ms
153,188 KB
testcase_08 AC 849 ms
156,732 KB
testcase_09 AC 846 ms
156,388 KB
testcase_10 AC 484 ms
115,428 KB
testcase_11 AC 488 ms
115,152 KB
testcase_12 AC 714 ms
132,144 KB
testcase_13 AC 680 ms
131,872 KB
testcase_14 AC 904 ms
161,832 KB
testcase_15 AC 839 ms
156,828 KB
testcase_16 AC 739 ms
132,132 KB
testcase_17 AC 476 ms
111,612 KB
testcase_18 AC 539 ms
115,436 KB
testcase_19 AC 616 ms
119,948 KB
testcase_20 AC 625 ms
120,120 KB
testcase_21 AC 619 ms
120,432 KB
testcase_22 AC 582 ms
119,204 KB
testcase_23 AC 802 ms
153,452 KB
testcase_24 AC 597 ms
119,360 KB
testcase_25 AC 519 ms
115,016 KB
testcase_26 AC 508 ms
115,128 KB
testcase_27 AC 571 ms
119,420 KB
testcase_28 AC 877 ms
160,728 KB
testcase_29 AC 625 ms
120,308 KB
testcase_30 AC 859 ms
156,424 KB
testcase_31 AC 571 ms
119,480 KB
testcase_32 AC 611 ms
119,840 KB
testcase_33 AC 421 ms
122,184 KB
testcase_34 AC 445 ms
135,840 KB
testcase_35 AC 573 ms
143,288 KB
testcase_36 AC 584 ms
162,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 998244353


class BIT:
    """ 1-indexで使用しないと無限ループするので注意 """

    def __init__(self, n):
        self.n = n
        self.bit = [0] * (n + 1)

    def update(self, idx, x):
        """ idx番目にxをO(logN)で加算する """
        while idx <= self.n:
            self.bit[idx] += x
            idx += idx & (-idx)

    def query(self, idx):
        """ 1 ~ idx番目までの区間和をO(logN)で返す """
        res = 0
        while idx > 0:
            res += self.bit[idx]
            idx -= idx & (-idx)
        return res

    def sec_sum(self, left, right):
        """ left ~ right番目までの区間和をO(logN)で返す """
        return self.query(right) - self.query(left - 1)

    def lower_bound(self, w):
        """ 区間和がw以上になる最小のindexをO(logN)で返す """
        if w <= 0:
            return 0
        x = 0
        k = 1 << self.n.bit_length()
        while k:
            if x + k <= self.n and self.bit[x + k] < w:
                w -= self.bit[x + k]
                x += k
            k //= 2
        return x + 1

    def debug(self):
        """ bitの状態を出力する """
        print(*[self.sec_sum(i, i) for i in range(1, self.n + 1)])


def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    comp = sorted(list(set(A)))

    bit_cnt = BIT(n + 10)
    bit_total = BIT(n + 10)
    cnt = [[0, 0] for _ in range(n)]
    total = [[0, 0] for _ in range(n)]
    for i in range(n):
        idx = bisect_left(comp, A[i]) + 1
        cnt[i][0] = bit_cnt.sec_sum(idx + 1, n)
        total[i][0] = bit_total.sec_sum(idx + 1, n) % mod
        bit_cnt.update(idx, 1)
        bit_total.update(idx, A[i])

    bit_cnt = BIT(n + 10)
    bit_total = BIT(n + 10)
    for i in reversed(range(n)):
        idx = bisect_left(comp, A[i]) + 1
        cnt[i][1] = bit_cnt.query(idx - 1)
        total[i][1] = bit_total.query(idx - 1) % mod
        bit_cnt.update(idx, 1)
        bit_total.update(idx, A[i])

    res = 0
    for i in range(n):
        left_cnt, right_cnt = cnt[i]
        left_tot, right_tot = total[i]
        if (not left_cnt) or (not right_cnt):
            continue
        res += left_cnt * right_tot % mod + right_cnt * left_tot % mod + A[i] * left_cnt % mod * right_cnt % mod
        res %= mod
    print(res)


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