結果
| 問題 |
No.956 Number of Unbalanced
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 14:16:57 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,267 bytes |
| コンパイル時間 | 444 ms |
| コンパイル使用メモリ | 82,384 KB |
| 実行使用メモリ | 269,632 KB |
| 最終ジャッジ日時 | 2025-06-12 14:17:03 |
| 合計ジャッジ時間 | 4,866 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 6 |
| other | TLE * 1 -- * 23 |
ソースコード
from collections import defaultdict
class FenwickTree:
def __init__(self, size):
self.size = size
self.tree = [0] * (self.size + 2)
def update(self, index, delta=1):
while index <= self.size:
self.tree[index] += delta
index += index & -index
def query(self, index):
res = 0
while index > 0:
res += self.tree[index]
index -= index & -index
return res
def main():
import sys
input = sys.stdin.read
data = input().split()
N = int(data[0])
A = list(map(int, data[1:N+1]))
total = 0
seen = set()
for x in A:
if x in seen:
continue
seen.add(x)
T = [1 if a == x else -1 for a in A]
S = [0] * (N + 1)
for i in range(1, N+1):
S[i] = S[i-1] + T[i-1]
shifted = [s + N for s in S]
max_val = 2 * N
ft = FenwickTree(max_val)
ft.update(shifted[0])
cnt = 0
for i in range(1, N+1):
current = shifted[i]
less = ft.query(current - 1)
cnt += less
ft.update(current)
total += cnt
print(total)
if __name__ == "__main__":
main()
gew1fw