結果

問題 No.696 square1001 and Permutation 5
ユーザー gew1fw
提出日時 2025-06-12 14:35:37
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 939 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 82,612 KB
実行使用メモリ 848,880 KB
最終ジャッジ日時 2025-06-12 14:36:04
合計ジャッジ時間 3,853 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 2
other MLE * 1 -- * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

class FenwickTree:
    def __init__(self, size):
        self.size = size
        self.tree = [0] * (size + 2)  # 1-based indexing

    def update_point(self, index, delta):
        while index <= self.size:
            self.tree[index] += delta
            index += index & -index

    def query_prefix(self, index):
        res = 0
        while index > 0:
            res += self.tree[index]
            index -= index & -index
        return res

n = int(input())
p = list(map(int, input().split()))

# Precompute factorials up to (n-1)!
fact = [1] * n
for i in range(1, n):
    fact[i] = fact[i-1] * i

fenwick = FenwickTree(n)
for i in range(1, n+1):
    fenwick.update_point(i, 1)

total = 0
for i in range(n):
    current_p = p[i]
    x = fenwick.query_prefix(current_p - 1)
    exponent = (n-1) - i
    if exponent < 0:
        exponent = 0
    total += x * fact[exponent]
    fenwick.update_point(current_p, -1)

print(total + 1)
0