DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Dadasquare

Back to problem

Editorial

Binary-search the minimum feasible square side length LLL. Since all point coordinates are integers, every partition of the points into four squares admits squares with integer-aligned sides; hence the optimal LLL is an integer in the range [0,2⋅109][0,2\cdot 10^9][0,2⋅109].

Let the bounding box of all points be [xmin⁡,xmax⁡]×[ymin⁡,ymax⁡][x_{\min},x_{\max}]\times[y_{\min},y_{\max}][xmin​,xmax​]×[y.

1. A square occupies a corner.

If two squares suffice, there is a solution with the squares anchored at opposite corners of the bounding box. Checking both diagonals takes O(N)O(N)O(N).

If three squares suffice, one square can be anchored at a corner: four extreme sides must be touched by three squares, so a square touches two extremes. For adjacent extremes, move the square to their common corner. For opposite extremes, the corresponding total extent is at most LLL, allowing an equivalent corner placement. Try all four corners and solve the residual two-square problem in O(N)O(N)O(N) total time.

Likewise, if any square in a four-square cover touches two extreme sides, a corner square can be chosen. Try the four corners and solve the residual three-square problems in O(N)O(N)O(N) total time.

2. Four squares touch four different sides.

Otherwise the four squares individually touch the left, right, top, and bottom sides. Denote them by SL,SR,ST,SBS_L,S_R,S_T,S_BSL​,SR​,ST​,. Anchor their corresponding edges at . By also checking the instance reflected vertically, it suffices to consider the case where lies to the left of .

Fix the height ttt of the upper edge of SLS_LSL​. Of the points not covered by SLS_LSL​, let aa be the minimum -coordinate. We may anchor the left edge of at , with its upper edge at . This is a greedy placement: moving left until it reaches the leftmost remaining point cannot lose any point needed by the subsequent squares.

Next, let bbb be the minimum xxx-coordinate among points still uncovered. Place SBS_BSB​ with left edge bbb and bottom edge at ymin⁡y_{\min}. The remaining points must fit inside a single square, which is equivalent to requiring both extents of their bounding box to be at most .

3. An O(Nlog⁡N)O(N\log N)O(NlogN) sweep.

Only points with xi≤xmin⁡+Lx_i\le x_{\min}+Lxi​≤xmin​+L can be covered by SLS_LS. Such a point belongs to exactly when , yielding at most two events per point.

The vertical coverage ranges of STS_TST​ and SBS_BSB​ are fixed: respectively y≥ymax⁡−Ly\ge y_{\max}-Ly≥ and within the bounding box. Classify every point by membership in the top range, bottom range, both, or neither.

Sort the points by xxx and build a segment tree. For the points currently outside SLS_LSL​, every node stores a bounding box separately for each of these four classes. Changing the activity of one point takes O(log⁡N)O(\log N)O(logN). Querying the bounding box of a union of classes restricted to an xx-interval also takes .

The current left edge aaa of STS_TST​ is the minimum xxx among active points. STS_TST removes top-range points with . The next minimum remaining is , the left edge of .

All points surviving STS_TST​ and SBS_BSB​ have x≥bx\ge bx≥b. Their bounding box is the union of three queries:

  • b≤x≤a+Lb\le x\le a+Lb≤x≤a+L: points in neither the top nor the bottom range.
  • max⁡(b,a+L+1)≤x≤b+L\max(b,a+L+1)\le x\le b+Lmax(b,a+L+: points not in the bottom range.

The boundaries use integer coordinates, so strict inequalities can be expressed by adding one. At each event height, test the state after activating the newly covered points, and also the state immediately after points leave the square. Thus all distinct active sets are considered.

There are at most 2N2N2N events, each requiring O(log⁡N)O(\log N)O(logN) time for updates and a constant number of queries. The sweep takes O(Nlog⁡N)O(N\log N)O(NlogN) time. Reflection changes this only by a constant factor.

Each feasibility test takes O(Nlog⁡N)O(N\log N)O(NlogN) time. Binary search gives O(Nlog⁡Nlog⁡109)O(N\log N\log 10^9)O(NlogNlog109) total time and space.

min​
,
ymax​
]
SB​
xmin⁡,xmax⁡,ymax⁡,ymin⁡x_{\min},x_{\max},y_{\max},y_{\min}xmin​,xmax​,ymax​,ymin​
STS_TST​
SBS_BSB​
a
xxx
STS_TST​
aaa
ymax⁡y_{\max}ymax​
STS_TST​
ymin​
LLL
L​
SLS_LSL​
yi≤t≤yi+Ly_i\le t\le y_i+Lyi​≤t≤yi​+L
ymax​−
L
y≤ymin⁡+Ly\le y_{\min}+Ly≤ymin​+L
x
O(log⁡N)O(\log N)O(logN)
​
x≤a+Lx\le a+Lx≤a+L
xxx
bbb
SBS_BSB​
1
)
≤
x≤
b+
L
  • x>b+Lx>b+Lx>b+L: points of all classes.
  • O(N)O(N)
    O(N)