結果

問題 No.1604 Swap Sort:ONE
ユーザー c-yanc-yan
提出日時 2021-07-16 23:46:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 703 bytes
コンパイル時間 113 ms
コンパイル使用メモリ 10,820 KB
実行使用メモリ 8,608 KB
最終ジャッジ日時 2023-09-20 16:15:23
合計ジャッジ時間 1,996 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,784 KB
testcase_01 AC 16 ms
7,820 KB
testcase_02 AC 16 ms
7,720 KB
testcase_03 AC 16 ms
7,892 KB
testcase_04 AC 15 ms
7,800 KB
testcase_05 AC 25 ms
8,512 KB
testcase_06 AC 26 ms
8,568 KB
testcase_07 AC 26 ms
8,440 KB
testcase_08 AC 26 ms
8,608 KB
testcase_09 AC 26 ms
8,492 KB
testcase_10 AC 27 ms
8,412 KB
testcase_11 AC 26 ms
8,588 KB
testcase_12 AC 27 ms
8,504 KB
testcase_13 AC 26 ms
8,576 KB
testcase_14 AC 26 ms
8,424 KB
testcase_15 AC 26 ms
8,432 KB
testcase_16 AC 26 ms
8,584 KB
testcase_17 AC 22 ms
8,116 KB
testcase_18 AC 21 ms
8,088 KB
testcase_19 AC 26 ms
8,544 KB
testcase_20 AC 21 ms
8,096 KB
testcase_21 AC 24 ms
8,476 KB
testcase_22 AC 24 ms
8,192 KB
testcase_23 AC 18 ms
8,340 KB
testcase_24 AC 17 ms
7,772 KB
testcase_25 AC 25 ms
8,344 KB
testcase_26 AC 21 ms
8,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BinaryIndexedTree:
    def __init__(self, size):
        self._data = [0] * size

    def add(self, i, x):
        data = self._data
        i += 1
        n = len(data)
        while i <= n:
            data[i - 1] += x
            i += i & -i

    def _sum(self, stop):
        data = self._data
        result = 0
        i = stop
        while i > 0:
            result += data[i - 1]
            i -= i & -i
        return result

    def range_sum(self, start, stop):
        return self._sum(stop) - self._sum(start)


N, *P = map(int, open(0).read().split())

bit = BinaryIndexedTree(N + 1)

result = 0
for x in P:
    result += bit.range_sum(x + 1, N + 1)
    bit.add(x, 1)
print(result)
0