結果
| 問題 |
No.696 square1001 and Permutation 5
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-04-15 21:09:01 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 788 bytes |
| コンパイル時間 | 187 ms |
| コンパイル使用メモリ | 82,376 KB |
| 実行使用メモリ | 848,864 KB |
| 最終ジャッジ日時 | 2025-04-15 21:14:26 |
| 合計ジャッジ時間 | 4,172 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 2 |
| other | MLE * 1 -- * 11 |
ソースコード
class FenwickTree:
def __init__(self, size):
self.n = size
self.tree = [0] * (self.n + 1)
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
n = int(input())
p = list(map(int, input().split()))
# Precompute factorial
fact = [1] * (n + 1)
for i in range(1, n + 1):
fact[i] = fact[i - 1] * i
ft = FenwickTree(n)
for i in range(1, n + 1):
ft.update(i, 1)
sum_rank = 0
for i in range(n):
current = p[i]
cnt = ft.query(current - 1)
sum_rank += cnt * fact[n - i - 1]
ft.update(current, -1)
print(sum_rank + 1)
lam6er