Square Count
Time limit1sMemory limit128 MB
Count all axis-aligned squares whose unit tiles lie in the union of rectangular rooms, where adjacent rooms connect through centered doors.
- Level
Hard8 of 10
- Topics
- Geometry, Implementation, Prefix sum, Brute force
- Solved
- No attempts yet
Problem
On the floor of each room of a museum, the tiles form a grid of unit squares. Besides the individual tiles, larger squares can be formed: any block of tiles (, , and so on) is also a square, and every such square must be added to the count. A square may also lie across two rooms, passing from one into an adjacent room through the opening between them.
For example, the two rooms of the first sample below contain 86 squares in total: 45 of size , 28 of size , and 13 of size . (The opening between those two rooms is only 3 squares wide.)
Given the rooms of the museum, count all of the squares.
Input
The input consists of multiple test cases. The first line of each test case is a positive integer , the number of rooms in that test case. Each of the next lines describes one room. Every room is a rectangle, given as
x1 y1 x2 y2
where and are the integer coordinates of two opposite corners. No two rooms overlap, although they may share a side. If a shared side has length , then a door of length exists between the two rooms, centered along the shared side, and a square may pass from one room into the other only through such a door. No square of any size overlaps more than two rooms. All and values are at most 1,000,000. A line containing ends the input and is not processed.
Output
For each test case, output one line in the form Case i: t, where is the test case number (starting from 1) and is the total number of squares. Every answer fits in a 32-bit integer.