Barn Expansion
InterviewTime limit1sMemory limit128 MB
Given up to 25000 non-overlapping axis-aligned rectangles, count how many touch no other rectangle at any corner or along any wall.
- Level
Medium7 of 10
- Topics
- Geometry, Sorting, Implementation
- Solved
- No attempts yet
Problem
Farmer John has rectangular barns () on his farm. Every barn has sides parallel to the - and -axes, and its corner coordinates are integers between and . The barns do not overlap, although they may share corners and/or sides with one another.
Because he has more cows to milk this year, Farmer John wants to expand some of his barns. A barn has room to expand only if it does not share a corner or a wall with any other barn — that is, all four of its walls can be pushed outward by at least some small amount without bumping into another barn. If two barns meet at a single corner, neither of them can expand.
Determine how many barns have room to expand.
Input
- Line 1: a single integer .
- Lines 2 to : four space-separated integers , , , describing one barn. Its lower-left corner is at and its upper-right corner is at , with and .
Output
- A single integer: the number of barns that have room to expand.
Hint
In the example, only the first two barns listed can expand. Each of the other three barns touches at least one other barn at a corner or along a wall, so none of them can expand.