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 account linking.

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

Rectangle Tiling

Back to problem

Editorial

Rectangles have three types: top-row only, bottom-row only, or both rows.

Once the selected both-row rectangles are fixed, they must be non-overlapping from left to right. In every gap between two consecutive both-row rectangles, top-only and bottom-only rectangles can be scheduled independently.

For one row, define next(x)(x)(x) as the smallest right endpoint among intervals whose left endpoint is greater than xxx. Repeatedly following next is exactly the usual earliest-finish greedy interval schedule. Make each endpoint xxx a child of next(x)(x)(x). When a sweep passes a right endpoint eee, exactly the starting points in the subtree of eee gain one additional greedily selectable interval.

Relevant starting points are 000 and the right endpoints of both-row rectangles. Build this tree for each row and assign DFS orders. A starting point sss becomes a static 2D point (p1(s),p2(s))(p_1(s),p_2(s))(p1​(s),p. A subtree update in the first row is a vertical strip update on these points, while one in the second row is a horizontal strip update.

Store the static points in a 2D KD tree, maintaining each node's bounding box, maximum value, and lazy increment. Strip updates take O(K)O(\sqrt K)O(K​). Sweep columns from left to right. At the left endpoint of a both-row rectangle, its DP value is the current global maximum plus one. After processing one-row intervals ending at the current coordinate, activate the current coordinate as a new starting point with the best DP value of both-row rectangles ending there.

The total complexity is O(KK+Klog⁡K)O(K\sqrt K+K\log K)O(KK​+KlogK). Solution written by GPT5.6

2
​
(
s
))