公式

B - 花壇の配置図 / Flowerbed Layout 解説 by harurun4635


一番下の式を考えます。

「\(N-1\) 個の \(X\) 座標の相異なる値の個数」は \(N\) 個のときと大きくは変わらないはずです。

具体的には \(X_i\) を取り除くとき

  • \(X_i\) をとるものが他に存在するなら変化しない
  • そうでないなら \(1\) 個減る

となります。

よって、上が高速に判定できればよいです。これは、頻度列などを管理しておけばよいです。値は大きいので連想配列などを使うとよいでしょう。


実装例

from collections import defaultdict

n = int(input())

xs = defaultdict(int)
ys = defaultdict(int)
xy = []
for i in range(n):
    x, y = map(int, input().split())
    xs[x] += 1
    ys[y] += 1
    xy.append((x, y))

for x, y in xy:
    cx = len(xs) - (xs[x] == 1)
    cy = len(ys) - (ys[y] == 1)
    print(cx * cy - (n - 1))

投稿日時:
最終更新: