結果

問題 No.559 swapAB列
ユーザー TakoKurageTakoKurage
提出日時 2020-03-02 09:21:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 841 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 10,804 KB
実行使用メモリ 7,888 KB
最終ジャッジ日時 2023-08-03 23:51:31
合計ジャッジ時間 1,041 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,732 KB
testcase_01 AC 17 ms
7,812 KB
testcase_02 AC 17 ms
7,784 KB
testcase_03 AC 16 ms
7,828 KB
testcase_04 AC 16 ms
7,832 KB
testcase_05 AC 16 ms
7,864 KB
testcase_06 AC 16 ms
7,780 KB
testcase_07 AC 16 ms
7,836 KB
testcase_08 AC 17 ms
7,888 KB
testcase_09 AC 16 ms
7,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    @staticmethod
    def inversion_number(A, N):
        memo = {n: i for i, n in enumerate(sorted(set(A)), 1)}
        bit = BIT(N)
        ans = 0
        for i, a in enumerate(A):
            bit.add(memo[a], 1)
            ans += i + 1 - bit.sum(memo[a])
        return ans

    def __str__(self):
        return "BIT: [{}]".format(
            ", ".join(str(self.sum(i + 1) - self.sum(i))
                      for i in range(self.size))
        )


S = input()
print(BIT.inversion_number(S, len(S)))
0