結果

問題 No.1300 Sum of Inversions
ユーザー HAPPAHAPPA
提出日時 2020-11-28 11:22:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,562 bytes
コンパイル時間 104 ms
コンパイル使用メモリ 11,188 KB
実行使用メモリ 94,540 KB
最終ジャッジ日時 2023-10-09 23:46:44
合計ジャッジ時間 4,239 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
12,816 KB
testcase_01 AC 17 ms
8,472 KB
testcase_02 AC 17 ms
8,436 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 sys

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


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()))

    B = []
    for idx, a in enumerate(A):
        B.append([a, idx])
    B.sort()

    A_comp = [0] * n
    prev = -1
    i = 0
    for a, idx in B:
        if prev != a:
            i += 1
        A_comp[idx] = i
        prev = a

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

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

    res = 0
    for i in range(n):
        left_cnt, right_cnt = cnt[i]
        left_tot, right_tot = total[i]
        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