#P15277. [MCO 2025] Rectangle Connections

    ID: 15295 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>并查集2025MCC/MCO(马来西亚)

[MCO 2025] Rectangle Connections

Description

MCO Land can be modelled as an infinite 2D grid. The rows of the grid are numbered 0,1,2,0,1,2,\ldots from bottom to top, and the columns are numbered 0,1,2,0,1,2,\ldots from left to right. The cell on column xx and row yy is called cell (x,y)(x,y).

MCO land consists of mm districts. Each district can be modelled as a rectangular subgrid in MCO land. The ii-th district is represented by four integers (xi,1,xi,2,yi,1,yi,2)(x_{i,1},x_{i,2},y_{i,1},y_{i,2}) where xi,1xi,2x_{i,1}\le x_{i,2} and yi,1yi,2y_{i,1}\le y_{i,2}. This means that the district consists of cells (x,y)(x, y) such that xi,1xxi,2x_{i,1}\le x\le x_{i,2} and yi,1yyi,2y_{i,1}\le y\le y_{i,2}.

Evirir, the leader of MCO Land, wants to build post offices and roads to service all districts of MCO Land.

Initially, there are no roads. Firstly, Evirir can connect as many districts as desired with horizontal or vertical roads (for free). Formally, two districts ii and jj can be connected by a road if and only if:

  • the intervals [xi,1,xi,2][x_{i,1},x_{i,2}] and [xj,1,xj,2][x_{j,1},x_{j,2}] intersect (possibly at the boundary), OR
  • the intervals [yi,1,yi,2][y_{i,1},y_{i,2}] and [yj,1,yj,2][y_{j,1},y_{j,2}] intersect (possibly at the boundary).

Notably, if two districts' rectangles overlap, then they can be connected by a road.

After that, Evirir can choose any number of districts and build a post office in each of them. In the end, all districts must be (directly or indirectly) connected to at least one post office. A district is connected to a post office if it contains a post office, or it is possible to travel from it to a district that contains a post office using roads.

Roads may cross over another road or district. However, if a road crosses another road, one cannot switch roads at the crossing point (the roads are overpasses).

What is the minimum number of post offices that Evirir needs to build?

Input Format

The first line contains an integer mm (1m21051\le m\le2\cdot{10}^5), the number of districts in MCO Land.

The ii-th of the next mm lines will contain four space-separated integers, xi,1x_{i,1}, xi,2x_{i,2}, yi,1y_{i,1}, and yi,2y_{i,2} (0xi,1xi,21090\le x_{i,1}\le x_{i,2}\le10^9, 0yi,1yi,21090\le y_{i,1}\le y_{i,2}\le10^9), representing the ii-th district (xi,1,xi,2,yi,1,yi,2)(x_{i,1},x_{i,2},y_{i,1},y_{i,2}).

Output Format

Output a single integer, the minimum number of post offices that need to be built.

5
1 2 1 2
2 4 7 8
0 0 4 4
6 7 3 4
7 8 4 6
2

Hint

Note

:::align{center} :::

In the example test case, there are three roads connecting districts 1 and 2, 3 and 4, and 4 and 5, respectively. Evirir can build two post offices at district 1 and 4. Then, districts 1 and 2 are connected to district 1's post office, and districts 3, 4, and 5 are connected to district 4's post office. It can be proven that 1 post office is not enough.

Note that even though the roads cross at cell (2,4)(2,4), you cannot switch between roads. For example, districts 1 and 3 are not connected.

Scoring

Subtask 1 (44 points): For some constant cc, yi,1=yi,2=cy_{i,1}=y_{i,2}=c for all 1im1\le i\le m.

Subtask 2 (1616 points): m2000m\le2000. 0xi,1,xi,2,yi,1,yi,220000\le x_{i,1},x_{i,2},y_{i,1},y_{i,2}\le2000 for all 1im1\le i\le m.

Subtask 3 (99 points): m2000m\le2000.

Subtask 4 (1212 points): xi,1=xi,2x_{i,1}=x_{i,2}, yi,1=yi,2y_{i,1}=y_{i,2} for all 1im1\le i\le m.

Subtask 5 (3232 points): yi,1=yi,2=iy_{i,1}=y_{i,2}=i for all 1im1\le i\le m.

Subtask 6 (1414 points): xi,1x(i+1),1x_{i,1}\le x_{(i+1),1}, yi,1y(i+1),1y_{i,1}\le y_{(i+1),1} for all 1im11\le i\le m-1.

Subtask 7 (1313 points): No additional constraints.