Square Count

Time limit1sMemory limit128 MB

Summary
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 1×11 \times 1 tiles, larger squares can be formed: any k×kk \times k block of tiles (2×22 \times 2, 3×33 \times 3, 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 1×11 \times 1, 28 of size 2×22 \times 2, and 13 of size 3×33 \times 3. (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 n≤1000n \le 1000, the number of rooms in that test case. Each of the next nn lines describes one room. Every room is a rectangle, given as

x1 y1 x2 y2

where (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) are the integer coordinates of two opposite corners. No two rooms overlap, although they may share a side. If a shared side has length m>2m > 2, then a door of length m−2m - 2 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 xx and yy values are at most 1,000,000. A line containing n=0n = 0 ends the input and is not processed.

Output

For each test case, output one line in the form Case i: t, where ii is the test case number (starting from 1) and tt is the total number of squares. Every answer fits in a 32-bit integer.

Examples1

  1. Example 1

    Input
    2
    0 0 9 3
    10 6 4 3
    3
    11 20 15 24
    11 17 15 20
    15 16 20 24
    0
    
    Expected output
    Case 1: 86
    Case 2: 152