結果

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

ソースコード

diff #

import sys

def main():
    n = int(sys.stdin.readline())
    p = list(map(int, sys.stdin.readline().split()))
    
    # 预计算阶乘
    fact = [1] * (n + 2)
    for i in range(1, n + 1):
        fact[i] = fact[i-1] * i
    
    # 定义BIT类
    class BIT:
        def __init__(self, size):
            self.size = size
            self.tree = [0] * (size + 2)
        
        def update(self, idx, delta):
            while idx <= self.size:
                self.tree[idx] += delta
                idx += idx & -idx
        
        def query(self, idx):
            res = 0
            while idx > 0:
                res += self.tree[idx]
                idx -= idx & -idx
            return res
    
    # 初始化BIT
    bit = BIT(n)
    for i in range(1, n+1):
        bit.update(i, 1)
    
    ans = 0
    for i in range(n):
        current = p[i]
        cnt = bit.query(current - 1)
        m = n - i - 1
        ans += cnt * fact[m]
        bit.update(current, -1)
    
    print(ans + 1)

if __name__ == "__main__":
    main()
0