結果
問題 | No.2327 Inversion Sum |
ユーザー |
![]() |
提出日時 | 2023-07-09 16:07:11 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 120 ms / 2,000 ms |
コード長 | 1,938 bytes |
コンパイル時間 | 520 ms |
コンパイル使用メモリ | 82,140 KB |
実行使用メモリ | 83,992 KB |
最終ジャッジ日時 | 2024-07-23 13:16:31 |
合計ジャッジ時間 | 4,258 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
import sysinput = sys.stdin.readlineclass Fenwick_Tree:def __init__(self, n):self._n = nself.data = [0] * ndef add(self, p, x):assert 0 <= p < self._np += 1while p <= self._n:self.data[p - 1] += xp += p & -pdef sum(self, l, r):assert 0 <= l <= r <= self._nreturn self._sum(r) - self._sum(l)def _sum(self, r):s = 0while r > 0:s += self.data[r - 1]r -= r & -rreturn sdef get_size(self, x):x = self.find(x)while r > 0:s += self.data[r - 1]r -= r & -rreturn sdef get(self, k):k += 1x, r = 0, 1while r < self._n:r <<= 1len = rwhile len:if x + len - 1 < self._n:if self.data[x + len - 1] < k:k -= self.data[x + len - 1]x += lenlen >>= 1return xN, M = map(int, input().split())mod = 998244353D = [-1] * Nspace = [0] * (N + 1)num = [1] * Nfor i in range(M):p, k = map(int, input().split())p, k = p - 1, k - 1D[k] = pnum[p] = 0Ac = [0] * (N + 1)for i in range(N):space[i + 1] = space[i] + (D[i] == -1)Ac[i + 1] = Ac[i] + num[i]nokori = N - Mfact = [1] * (N + 1)for i in range(N):fact[i + 1] = fact[i] * (i + 1)fact[i + 1] %= modfact.append(0)ans = 0if nokori >= 2:ans += (nokori * (nokori - 1)//2)**2 * fact[nokori - 2]ans %= modT = Fenwick_Tree(N)for i in range(N):if D[i] != -1:ans += T.sum(D[i], N) * fact[nokori]left = space[i]right = nokori - leftans += right * Ac[D[i] + 1] * fact[nokori - 1]ans += left * (Ac[-1] - Ac[D[i] + 1]) * fact[nokori - 1]ans %= modT.add(D[i], 1)print(ans)