結果

問題 No.1604 Swap Sort:ONE
コンテスト
ユーザー 学ぶマン
提出日時 2025-11-11 22:20:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 1,127 bytes
コンパイル時間 1,981 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 78,768 KB
最終ジャッジ日時 2025-11-11 22:21:04
合計ジャッジ時間 4,352 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing
class FenwickTree:

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.data = [0] * n

    def add(self, p: int, x: typing.Any) -> None:
        assert 0 <= p < self._n

        p += 1
        while p <= self._n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, left: int, right: int) -> typing.Any:
        assert 0 <= left <= right <= self._n

        return self._sum(right) - self._sum(left)

    def _sum(self, r: int) -> typing.Any:
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r

        return s

# リストを受けて転倒数を出す関数
def count_inversion(li:list):
    # リストと要素数
    res = 0
    limit = max(li)
    ft = FenwickTree(limit + 1) # 一番右のしきり・・limit + 1
    for num in li:
        # 自分より右側にいる出席者数をカウントする(区間和)
        res += ft.sum(num + 1, limit + 1)
        # 自分も席に座る
        ft.add(num, 1)

    return res

N = int(input())
P = list(map(int, input().split()))

print(count_inversion(P))
0