結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-12-12 21:47:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 143 ms / 2,500 ms
コード長 1,084 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 11,004 KB
実行使用メモリ 9,860 KB
最終ジャッジ日時 2023-09-28 15:16:54
合計ジャッジ時間 2,059 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,292 KB
testcase_01 AC 17 ms
8,308 KB
testcase_02 AC 16 ms
8,344 KB
testcase_03 AC 16 ms
8,204 KB
testcase_04 AC 17 ms
8,296 KB
testcase_05 AC 17 ms
8,188 KB
testcase_06 AC 18 ms
8,308 KB
testcase_07 AC 20 ms
8,396 KB
testcase_08 AC 25 ms
8,304 KB
testcase_09 AC 16 ms
8,320 KB
testcase_10 AC 17 ms
8,312 KB
testcase_11 AC 143 ms
9,808 KB
testcase_12 AC 141 ms
9,860 KB
testcase_13 AC 16 ms
8,268 KB
testcase_14 AC 16 ms
8,292 KB
testcase_15 AC 17 ms
8,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#BIT
class BinaryIndexedTree():
    def __init__(self, n):
        self.n = 1 << (n.bit_length())
        self.BIT = [0] * (self.n + 1)

    def build(self, init_lis):
        for i, v in enumerate(init_lis):
            self.add(i, v)

    def add(self, i, x):
        i += 1
        while i <= self.n:
            self.BIT[i] += x
            i += i & -i
    
    def sum(self, l, r):
        return self._sum(r) - self._sum(l)

    def _sum(self, i):
        res = 0
        while i > 0:
            res += self.BIT[i]
            i -= i & -i
        return res

    def binary_search(self, x):
        i = self.n
        while True:
            if i & 1:
                if x > self.BIT[i]:
                    i += 1
                break
            if x > self.BIT[i]:
                x -= self.BIT[i]
                i += (i & -i) >> 1
            else:
                i -= (i & -i) >> 1
        return i

n = int(input())
a = [int(input()) for _ in range(n)]
BIT = BinaryIndexedTree(n)
ans = 0
for i in range(n):
    ans += i - BIT._sum(a[i])
    BIT.add(a[i], 1)
print(ans)
0