結果
| 問題 |
No.956 Number of Unbalanced
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 20:56:17 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,578 bytes |
| コンパイル時間 | 208 ms |
| コンパイル使用メモリ | 81,724 KB |
| 実行使用メモリ | 292,848 KB |
| 最終ジャッジ日時 | 2025-03-20 20:56:28 |
| 合計ジャッジ時間 | 4,311 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 6 |
| other | TLE * 1 -- * 23 |
ソースコード
import bisect
from collections import defaultdict
def main():
import sys
input = sys.stdin.read().split()
n = int(input[0])
a = list(map(int, input[1:n+1]))
counter = defaultdict(int)
for num in a:
counter[num] += 1
threshold = 200
total = 0
for num in counter:
m = counter[num]
s = [1 if x == num else -1 for x in a]
prefix = [0]
for x in s:
prefix.append(prefix[-1] + x)
sorted_unique_prefix = sorted(set(prefix))
mapping = {v: i + 1 for i, v in enumerate(sorted_unique_prefix)}
max_rank = len(sorted_unique_prefix) + 2
fenwick = [0] * (max_rank + 1)
def update(idx, val=1):
while idx <= max_rank:
fenwick[idx] += val
idx += idx & -idx
def query(idx):
res = 0
while idx > 0:
res += fenwick[idx]
idx -= idx & -idx
return res
total_current = 0
current_sum = 0
update(mapping[0])
for i in range(len(s)):
current_sum += s[i]
p = current_sum
if p not in mapping:
rank = bisect.bisect_left(sorted_unique_prefix, p) + 1
else:
rank = mapping[p]
count = query(rank - 1)
total_current += count
update(rank)
total += total_current
print(total)
if __name__ == '__main__':
main()
lam6er