結果
問題 |
No.696 square1001 and Permutation 5
|
ユーザー |
![]() |
提出日時 | 2025-06-12 18:05:10 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,015 bytes |
コンパイル時間 | 190 ms |
コンパイル使用メモリ | 82,444 KB |
実行使用メモリ | 849,268 KB |
最終ジャッジ日時 | 2025-06-12 18:06:30 |
合計ジャッジ時間 | 4,282 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 2 |
other | MLE * 1 -- * 11 |
ソースコード
class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 2) # Using 1-based indexing def update(self, idx, delta): while idx <= self.n: 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 def main(): import sys input = sys.stdin.read().split() n = int(input[0]) p = list(map(int, input[1:n+1])) ft = FenwickTree(n) for i in range(1, n+1): ft.update(i, 1) # Precompute factorials where factorials[i] = (n-1 -i)! factorials = [1] * n for i in range(n-2, -1, -1): factorials[i] = factorials[i+1] * (n-1 - i) sum_rank = 0 for i in range(n): x = p[i] c = ft.query(x - 1) sum_rank += c * factorials[i] ft.update(x, -1) print(sum_rank + 1) if __name__ == "__main__": main()