結果
問題 | No.742 にゃんにゃんにゃん 猫の挨拶 |
ユーザー |
![]() |
提出日時 | 2023-02-13 01:16:57 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 113 ms / 2,500 ms |
コード長 | 1,175 bytes |
コンパイル時間 | 151 ms |
コンパイル使用メモリ | 81,716 KB |
実行使用メモリ | 77,856 KB |
最終ジャッジ日時 | 2024-07-16 08:15:24 |
合計ジャッジ時間 | 1,870 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
class 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(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 = int(input())TL, TR = Fenwick_Tree(N + 1), Fenwick_Tree(N + 1)ans = 0for i in range(N):M = int(input())M -= 1if M >= i:ans += TL.sum(M, N + 1)else:ans += TL.sum(M, N + 1)ans += TR.sum(M, N + 1)if M >= i:TL.add(M, 1)else:TR.add(M, 1)print(ans)