結果

問題 No.3469 ジャッジ結果の逆転数
コンテスト
ユーザー urunea
提出日時 2026-03-06 22:52:13
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 1,177 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 295 ms
コンパイル使用メモリ 85,684 KB
実行使用メモリ 151,068 KB
最終ジャッジ日時 2026-03-06 22:52:18
合計ジャッジ時間 3,626 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import bisect

class BIT:
    # 0-idx
    # (i, j)は両端点含む。
    def __init__(self, n):
        self.n = n
        self.a = [0] * (n + 1)

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

    def sum_sub(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.a[i]
            i -= i & (-i)
        return s
    
    def sum(self, i, j):
        return self.sum_sub(j) - self.sum_sub(i - 1)

    # a[0]+a[1]+...+a[i] >= x となる最小のiを返す (a[k] >= 0が前提)
    def lower_bound(self, x):
        if x <= 0:
            return 0
        i = 0
        r = 1
        while r < self.n:
            r <<= 1
        length = r
        while length > 0:
            if i + length <= self.n and self.a[i + length] < x:
                x -= self.a[i + length]
                i += length
            length >>= 1
        return i

N=int(input())
A=list(map(int, input().split()))
bit=BIT(N)
As = sorted(set(A))
ans = 0
for i in range(N):
    idx = bisect.bisect_left(As, A[i])
    if idx + 1 < N:
        ans += bit.sum(idx+1, N-1)
    bit.add(idx, 1)

print(ans)
0