結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー raven7959raven7959
提出日時 2021-10-23 16:47:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 93 ms / 2,500 ms
コード長 1,088 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 81,736 KB
実行使用メモリ 76,552 KB
最終ジャッジ日時 2023-10-25 23:53:13
合計ジャッジ時間 2,143 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,520 KB
testcase_01 AC 36 ms
53,520 KB
testcase_02 AC 37 ms
53,520 KB
testcase_03 AC 36 ms
53,520 KB
testcase_04 AC 42 ms
53,520 KB
testcase_05 AC 41 ms
53,520 KB
testcase_06 AC 50 ms
61,468 KB
testcase_07 AC 70 ms
72,952 KB
testcase_08 AC 69 ms
72,952 KB
testcase_09 AC 37 ms
53,520 KB
testcase_10 AC 36 ms
53,520 KB
testcase_11 AC 93 ms
76,552 KB
testcase_12 AC 92 ms
76,548 KB
testcase_13 AC 37 ms
53,520 KB
testcase_14 AC 37 ms
53,520 KB
testcase_15 AC 37 ms
53,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self._n = n
        self.data = [0]*(n+1)

    def add(self, p, x):  # pは0-indexed
        assert 0 <= p < self._n
        p += 1
        while p <= self._n:
            self.data[p] += x
            p += p & -p

    def sum_range(self, l, r):  # l,rは0-indexedで閉区間[l,r]の区間和を求める
        assert 0 <= l <= r <= self._n
        return self._sum(r+1)-self._sum(l)

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

    def lower_bound(self, x):  # a0+a1+...+ai>=xなるiのminを求める(0-indexed)
        lo = 1
        hi = self._n
        ret = 1 << 60
        while lo <= hi:
            m = (lo+hi)//2
            if self._sum(m) >= x:
                ret = min(ret, m)
                hi = m-1
            else:
                lo = m+1
        return ret-1


N = int(input())
M = [int(input())-1 for _ in range(N)]
B = BIT(N)
ans = 0
for i in range(N):
    num = B.sum_range(0, M[i])
    ans += i-num
    B.add(M[i], 1)
print(ans)
0