#P2449. Connected Rectangles Blocks

    ID: 15720 Type: Default 1000ms 256MiB

Connected Rectangles Blocks

Connected Rectangles Blocks

Given n rectangles on a plane, where each rectangle has integer coordinates and its sides are parallel to the coordinate axes, we define a block as follows:

  1. Every rectangle is considered a block on its own.
  2. If two blocks share a common segment (i.e. a line segment of positive length, not just a point), then they merge into a single block. Otherwise, they remain separate.

Your task is to read the rectangle data, determine the number of distinct blocks, and output that number.

Note on intersection: Two rectangles are connected if the intersection of their boundaries or interiors forms a line segment of positive length. For two rectangles A with coordinates [x1, y1, x2, y2] and B with coordinates [x3, y3, x4, y4], define $$L_x = \min(x_2,x_4)-\max(x_1,x_3)$$ $$L_y = \min(y_2,y_4)-\max(y_1,y_3)$$ They are connected if and only if \(L_x \ge 0\) and \(L_y \ge 0\) and at least one of \(L_x\) or \(L_y\) is strictly positive.

inputFormat

The input is read from the standard input and has the following format:

  • The first line contains one integer n (1 ≤ n ≤ 104) representing the number of rectangles.
  • Each of the next n lines contains 4 integers: x1 y1 x2 y2, where (x1, y1) and (x2, y2) are the coordinates of the bottom-left and top-right vertices of the rectangle respectively. It is guaranteed that x1 < x2 and y1 < y2.

outputFormat

Output a single integer representing the number of distinct connected blocks of rectangles.

The output should be written to the standard output.

sample

3
0 0 2 2
2 0 4 2
5 5 7 7
2