結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー 👑 rin204rin204
提出日時 2022-07-10 16:39:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 122 ms / 2,500 ms
コード長 1,018 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 87,280 KB
実行使用メモリ 78,776 KB
最終ジャッジ日時 2023-09-01 23:37:18
合計ジャッジ時間 2,873 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,272 KB
testcase_01 AC 70 ms
71,324 KB
testcase_02 AC 71 ms
71,532 KB
testcase_03 AC 69 ms
71,348 KB
testcase_04 AC 74 ms
71,120 KB
testcase_05 AC 76 ms
75,896 KB
testcase_06 AC 81 ms
76,000 KB
testcase_07 AC 98 ms
77,716 KB
testcase_08 AC 102 ms
77,912 KB
testcase_09 AC 70 ms
71,428 KB
testcase_10 AC 70 ms
71,412 KB
testcase_11 AC 121 ms
78,508 KB
testcase_12 AC 122 ms
78,776 KB
testcase_13 AC 71 ms
71,540 KB
testcase_14 AC 71 ms
71,148 KB
testcase_15 AC 70 ms
71,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    def __init__(self, n):
        self.size = n
        self.n0 = 1 << (n.bit_length() - 1)
        self.tree = [0] * (n + 1)
    
    def range_sum(self, l, r):
        return self.sum(r - 1) - self.sum(l - 1)
        
    def sum(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
        
    def get(self, i):
        return self.sum(i) - self.sum(i - 1)
 
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
         
    def lower_bound(self, x):
        pos = 0
        plus = self.n0
        while plus > 0:
            if pos + plus <= self.size and self.tree[pos + plus] < x:
                x -= self.tree[pos + plus]
                pos += plus
            plus //= 2
        return pos

n = int(input())
A = [int(input()) for _ in range(n)]
bit = Bit(n + 1)
ans = 0
for a in A:
    ans += bit.range_sum(a, n + 1)
    bit.add(a, 1)
print(ans)
0