結果
| 問題 |
No.956 Number of Unbalanced
|
| コンテスト | |
| ユーザー |
qwewe
|
| 提出日時 | 2025-05-14 12:48:38 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,309 bytes |
| コンパイル時間 | 269 ms |
| コンパイル使用メモリ | 82,040 KB |
| 実行使用メモリ | 295,068 KB |
| 最終ジャッジ日時 | 2025-05-14 12:50:00 |
| 合計ジャッジ時間 | 4,352 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 6 |
| other | TLE * 1 -- * 23 |
ソースコード
import sys
import bisect
from collections import defaultdict
class FenwickTree:
def __init__(self, size):
self.n = size
self.tree = [0] * (self.n + 2)
def update(self, idx, delta=1):
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
def main():
input = sys.stdin.read().split()
n = int(input[0])
a = list(map(int, input[1:n+1]))
pos_dict = defaultdict(list)
for i in range(n):
pos_dict[a[i]].append(i)
total = 0
for x in pos_dict:
prefix = [0] * (n + 1)
for i in range(1, n + 1):
prefix[i] = prefix[i-1] + (1 if a[i-1] == x else -1)
# Compress prefix values
sorted_prefix = sorted(set(prefix))
rank = {v: i+1 for i, v in enumerate(sorted_prefix)}
size = len(sorted_prefix)
ft = FenwickTree(size)
res = 0
for val in prefix:
idx = bisect.bisect_left(sorted_prefix, val)
res += ft.query(idx)
ft.update(rank[val])
total += res
print(total)
if __name__ == "__main__":
main()
qwewe