結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-15 22:23:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 158 ms / 2,500 ms
コード長 1,140 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 11,952 KB
実行使用メモリ 10,160 KB
最終ジャッジ日時 2023-10-20 06:50:37
合計ジャッジ時間 1,912 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,144 KB
testcase_01 AC 30 ms
10,144 KB
testcase_02 AC 31 ms
10,144 KB
testcase_03 AC 30 ms
10,144 KB
testcase_04 AC 28 ms
10,144 KB
testcase_05 AC 29 ms
10,144 KB
testcase_06 AC 30 ms
10,144 KB
testcase_07 AC 31 ms
10,144 KB
testcase_08 AC 36 ms
10,160 KB
testcase_09 AC 30 ms
10,144 KB
testcase_10 AC 29 ms
10,144 KB
testcase_11 AC 158 ms
10,160 KB
testcase_12 AC 156 ms
10,160 KB
testcase_13 AC 28 ms
10,144 KB
testcase_14 AC 28 ms
10,144 KB
testcase_15 AC 28 ms
10,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class fenwick_tree(object):
    def __init__(self, n):
        self.n = n
        self.log = n.bit_length()
        self.data = [0] * n

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

    def add(self, p, x):
        """ a[p] += xを行う"""
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        """a[l] + a[l+1] + .. + a[r-1]を返す"""
        return self.__sum(r) - self.__sum(l)

    def lower_bound(self, x):
        """a[0] + a[1] + .. a[i] >= x となる最小のiを返す"""
        if x <= 0:
            return -1
        i = 0
        k = 1 << self.log
        while k:
            if i + k <= self.n and self.data[i + k - 1] < x:
                x -= self.data[i + k - 1]
                i += k
            k >>= 1
        return i


n = int(input())
bit = fenwick_tree(n)

ans = 0
for _ in range(n):
    m = int(input()) - 1
    ans += bit.sum(m, n)
    bit.add(m, 1)
print(ans)
0