結果
問題 | No.742 にゃんにゃんにゃん 猫の挨拶 |
ユーザー |
![]() |
提出日時 | 2022-06-18 00:54:55 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 89 ms / 2,500 ms |
コード長 | 1,195 bytes |
コンパイル時間 | 337 ms |
コンパイル使用メモリ | 82,228 KB |
実行使用メモリ | 77,188 KB |
最終ジャッジ日時 | 2024-10-09 11:29:34 |
合計ジャッジ時間 | 1,977 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
# dataの長さは1番目からnを2進数にしたときのLSBに相当class Binary_Indexed_Tree:def __init__(self, n) -> None:self._n = nself.data = [0] * (n+1)self.depth = n.bit_length()# al〜arに一律加算ならいもす法でlにx加算、r+1に-x加算すれば良い# aiの値の取得はiまでのsumdef add(self, p, x) -> None:"""任意の要素ai←ai+xを行う O(logn)"""# pのindexが0以上n-1以下を保証assert 0 <= p < self._n# 1-indexedに変換p += 1while p <= self._n:# 加算self.data[p-1] += x# LSBの加算p += p & (-p)# 区間[l, r)def sum(self, l, r) -> int:assert 0 <= l <= r <= self._nreturn self._sum(r) - self._sum(l)def _sum(self, d) -> int:sm = 0while d > 0:sm += self.data[d-1]# LSB減算d -= d & (-d)return smN = int(input())BIT = Binary_Indexed_Tree(N+1)M = [int(input()) for _ in range(N)]M = M[::-1]res = 0for num in M:BIT.add(num, 1)res += BIT._sum(num)print(res)