結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー rlangevinrlangevin
提出日時 2023-02-13 01:16:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 145 ms / 2,500 ms
コード長 1,175 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 80,340 KB
最終ジャッジ日時 2023-09-23 08:27:07
合計ジャッジ時間 2,904 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,228 KB
testcase_01 AC 70 ms
71,192 KB
testcase_02 AC 71 ms
71,196 KB
testcase_03 AC 69 ms
71,232 KB
testcase_04 AC 80 ms
75,484 KB
testcase_05 AC 81 ms
76,064 KB
testcase_06 AC 82 ms
75,516 KB
testcase_07 AC 93 ms
76,116 KB
testcase_08 AC 121 ms
78,812 KB
testcase_09 AC 73 ms
71,324 KB
testcase_10 AC 73 ms
71,488 KB
testcase_11 AC 136 ms
78,372 KB
testcase_12 AC 145 ms
80,340 KB
testcase_13 AC 72 ms
71,252 KB
testcase_14 AC 70 ms
71,324 KB
testcase_15 AC 70 ms
71,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

    def sum(self, l, r):
        assert 0 <= l <= r <= self._n
        return self._sum(r) - self._sum(l)

    def _sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s
    
    def get(self, k):
        k += 1
        x, r = 0, 1
        while r < self._n:
            r <<= 1
        len = r
        while len:
            if x + len - 1 < self._n:
                if self.data[x + len - 1] < k:
                    k -= self.data[x + len - 1]
                    x += len
            len >>= 1
        return x

N = int(input())
TL, TR = Fenwick_Tree(N + 1), Fenwick_Tree(N + 1)
ans = 0
for i in range(N):
    M = int(input())
    M -= 1
    if M >= i:
        ans += TL.sum(M, N + 1)
    else:
        ans += TL.sum(M, N + 1)
        ans += TR.sum(M, N + 1)
        
    if M >= i:
        TL.add(M, 1)
    else:
        TR.add(M, 1)

print(ans)
0