結果

問題 No.1604 Swap Sort:ONE
ユーザー brthyyjpbrthyyjp
提出日時 2021-07-18 16:33:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 1,353 bytes
コンパイル時間 1,018 ms
コンパイル使用メモリ 87,060 KB
実行使用メモリ 76,984 KB
最終ジャッジ日時 2023-09-22 18:51:26
合計ジャッジ時間 5,407 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,440 KB
testcase_01 AC 76 ms
71,308 KB
testcase_02 AC 75 ms
71,312 KB
testcase_03 AC 76 ms
71,192 KB
testcase_04 AC 75 ms
71,340 KB
testcase_05 AC 95 ms
76,752 KB
testcase_06 AC 93 ms
76,912 KB
testcase_07 AC 94 ms
76,792 KB
testcase_08 AC 93 ms
76,732 KB
testcase_09 AC 92 ms
76,612 KB
testcase_10 AC 93 ms
76,568 KB
testcase_11 AC 92 ms
76,560 KB
testcase_12 AC 92 ms
76,764 KB
testcase_13 AC 93 ms
76,744 KB
testcase_14 AC 91 ms
76,984 KB
testcase_15 AC 94 ms
76,780 KB
testcase_16 AC 93 ms
76,632 KB
testcase_17 AC 91 ms
76,852 KB
testcase_18 AC 92 ms
76,772 KB
testcase_19 AC 94 ms
76,752 KB
testcase_20 AC 93 ms
76,552 KB
testcase_21 AC 94 ms
76,632 KB
testcase_22 AC 92 ms
76,700 KB
testcase_23 AC 85 ms
75,780 KB
testcase_24 AC 84 ms
75,712 KB
testcase_25 AC 92 ms
76,792 KB
testcase_26 AC 92 ms
76,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0]*(self.n+1) # 1-indexed

    def init(self, init_val):
        for i, v in enumerate(init_val):
            self.add(i, v)

    def add(self, i, x):
        # i: 0-indexed
        i += 1 # to 1-indexed
        while i <= self.n:
            self.bit[i] += x
            i += (i & -i)

    def sum(self, i, j):
        # return sum of [i, j)
        # i, j: 0-indexed
        return self._sum(j) - self._sum(i)

    def _sum(self, i):
        # return sum of [0, i)
        # i: 0-indexed
        res = 0
        while i > 0:
            res += self.bit[i]
            i -= i & (-i)
        return res

    def lower_bound(self, x):
        s = 0
        pos = 0
        depth = self.n.bit_length()
        v = 1 << depth
        for i in range(depth, -1, -1):
            k = pos + v
            if k <= self.n and s + self.bit[k] < x:
                    s += self.bit[k]
                    pos += v
            v >>= 1
        return pos

    def __str__(self): # for debug
        arr = [self.sum(i,i+1) for i in range(self.n)]
        return str(arr)

n = int(input())
P = list(map(int, input().split()))
P = [p-1 for p in P]
A = [0]*n
for i, p in enumerate(P):
    A[p] = i

bit = BIT(n)
ans = 0
for a in A:
    ans += bit.sum(a+1, bit.n)
    bit.add(a, 1)
print(ans)
0