Lego

It's Christmas morning, and you've got what you wanted: a box of Lego! (Okay, maybe not, but better than nothing)

Legos are pretty fun to tinker with, and you've decided to build some sort of shape.
(For the sake of this problem, let's say your shape is basically 2-dimensional - it'll be a slab)

But once you pick it up, you discover that you didn't plan it properly, and your wonderful shape just falls apart.
Now, you're planning to build something big, and so you're going to use the computer to help you.
Write a program, that given the layout of a Lego design, outputs the number of pieces it would break into if picked up.
(Assume that the bricks bind together perfectly)

The Legos will be built on a x-y coordinate plane, with (0,0) being the bottom left corner.
The blocks are flat on your carpet, so a block will never 'fall down'.
(If you haven't seen a Lego brick before: A Lego brick has grooves on its top that match with notches on the bottom.
If a groove and a notch bind, the bricks will stay together. See the diagram.
A brick will bind with another brick securely even if just a single notch touches another groove.)

Input:

N (the number of Lego pieces) ≤ 100,000
N lines, each with 4 integers x1, y1, x2, y2 (0 < x2 ≤ 109, 0 < y2 ≤ 109, x1 < x2, y1 < y2)
This means that there is a brick with bottom left corner (x1,y1) and top right corner (x2,y2).
No bricks will overlap.

Output:

The number of separate pieces these blocks form.

Sample Input:

4
0 0 2 2
1 2 3 4
2 0 4 2
4 0 6 2

Sample Output:

2

Blocks #1,2,3 are joined securely.
However, #4 is just hanging around.


All Submissions
Best Solutions


Point Value: 20 (partial)
Time Limit: 1.00s
Memory Limit: 32M
Added: Dec 20, 2008
Author: hansonw1

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

Comments (Search)

For further clarification, consider the following case:
3 
0 0 2 2
0 4 2 6
2 2 4 4

The answer will be 3. (The second block won't fall, and the 3rd block isn't connected)