Doors and Penguins

Time limit1sMemory limit128 MB

Summary
Given axis-parallel rectangles labeled Doors or Penguins, decide whether one straight line avoiding all rectangles can separate the two groups.
Level

Hard8 of 10

Topics
Geometry, Divide and conquer, Greedy, Sorting
Solved
No attempts yet

Problem

The organizers of a large computing conference have invited several vendors to set up booths in a big exhibition hall to showcase their latest products. After the booths were assigned and built, the organizers realized an important detail: each vendor supports exactly one of two operating systems — Doors or Penguins — never both, and a vendor supporting one system does not want a booth next to a vendor supporting the other.

The booths have already been placed and cannot be moved or reassigned. To keep the two groups apart, the organizers have portable partition screens that can build a single straight wall of any length. The wall must not touch any booth (it may come arbitrarily close to touching one). Determine whether the two groups of vendors can be separated by one such straight wall.

Input

The input contains several test cases.

Each case begins with a line holding two integers DD and PP separated by a single space: the number of vendors supporting Doors and the number supporting Penguins, respectively (1≤D,P≤5001 \le D, P \le 500).

The next DD lines describe the Doors booths, followed by PP lines describing the Penguins booths. Each booth is given by four positive integers x1 y1 x2 y2x_1\ y_1\ x_2\ y_2, where (x1,y1)(x_1, y_1) is the south-west corner and (x2,y2)(x_2, y_2) is the north-east corner, with x1<x2x_1 < x_2 and y1<y2y_1 < y_2. Every booth is an axis-parallel rectangle.

The exhibition hall has its south-west corner at (0,0)(0, 0) and its north-east corner at (15000,15000)(15000, 15000). All booths lie strictly inside the hall and do not touch its walls. No two booths overlap or touch each other.

The input ends with a line containing D=P=0D = P = 0, which is not processed.

Output

For each case, print the case number (starting from 1) followed by a colon and a space, then print

It is possible to separate the two groups of vendors.

if the two groups can be separated by a single straight wall, or

It is not possible to separate the two groups of vendors.

otherwise. Print a blank line between consecutive cases.

Examples3

  1. Example 1

    Input
    3 3
    10 40 20 50
    50 80 60 90
    30 60 40 70
    30 30 40 40
    50 50 60 60
    10 10 20 20
    2 1
    10 10 20 20
    40 10 50 20
    25 12 35 40
    0 0
    
    Expected output
    Case 1: It is possible to separate the two groups of vendors.
    
    Case 2: It is not possible to separate the two groups of vendors.
    
  2. Example 2

    Input
    1 1
    100 100 200 200
    1000 1000 1100 1100
    0 0
    
    Expected output
    Case 1: It is possible to separate the two groups of vendors.
    
  3. Example 3

    Input
    2 1
    10 10 20 20
    40 10 50 20
    25 12 35 40
    0 0
    
    Expected output
    Case 1: It is not possible to separate the two groups of vendors.