公式

E - Total Area of Bounding Boxes 解説 by evima


Although the problem statement specifies that subset sizes must be at least \(2\), if we consider the area of bounding boxes for subsets of size \(1\) or less to be \(0\), then calculating the sum over all subsets gives the same answer.

We need to calculate, for each unit grid square, the number of cases where it is included in a bounding box, and then find the sum.

For a square with bottom-left coordinate \((X,Y)\) and top-right coordinate \((X+1,Y+1)\) to be included in the bounding box of \(S\), the following conditions are necessary and sufficient:

  • At least one point with \(x\)-coordinate \(\leq X\) is included in \(S\)
  • At least one point with \(x\)-coordinate \(\geq X+1\) is included in \(S\)
  • At least one point with \(y\)-coordinate \(\leq Y\) is included in \(S\)
  • At least one point with \(y\)-coordinate \(\geq Y+1\) is included in \(S\)

Using the inclusion-exclusion principle, we need to calculate the following two types of values (and their rotations):

  • (a) The number of sets consisting only of points with \(x\)-coordinate \(\leq X\)
  • (b) The number of sets consisting only of points with \(x\)-coordinate \(\leq X\) and \(y\)-coordinate \(\leq Y\)

Since calculating the sum of (a) over all unit grid squares is straightforward, we consider how to calculate the sum of (b).

By performing a plane sweep in the \(x\)-axis direction, we need to design a data structure that can efficiently handle the following queries:

  • Add a point with \(y\)-coordinate \(Y\)
  • Calculate \(\sum_{1 \le y \lt N} 2^{C_y}\), where \(C_y\) is the number of points with \(y\)-coordinate \(\leq Y\)

This can be processed in \(O(\log N)\) per query using Lazy Segtree.

The overall time complexity is \(O(N \log N)\). (Code)

投稿日時:
最終更新: