2019 Multi-University Training Contest 8 Calabash and Landlord

在一个无限的二维平面上,地主通过设置两个正交的矩形围栏将其土地划分为若干个连通区域。本篇探讨了如何计算这些围栏形成的非空连通组件的数量,包括有限和无限的区域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Calabash and Landlord

Problem Description

Calabash is the servant of a landlord. The landlord owns a piece of land, which can be regarded as an infinite 2D plane.

One day the landlord set up two orthogonal rectangular-shaped fences on his land. He asked Calabash a simple problem: how many nonempty connected components is my land divided into by these two fences, both finite and infinite? Calabash couldn’t answer this simple question. Please help him!

Recall that a connected component is a maximal set of points not occupied by the fences, and every two points in the set are reachable without crossing the fence.

Input

The first line of input consists of a single integer T (1≤T≤10000), the number of test cases.

Each test case contains two lines, specifying the two rectangles. Each line contains four integers x1,y1,x2,y2 (0≤x1,y1,x2,y2≤109,x1<x2,y1<y2), where (x1,y1),(x2,y2) are the Cartesian coordinates of two opposite vertices of the rectangular fence. The edges of the rectangles are parallel to the coordinate axes. The edges of the two rectangles may intersect, overlap, or even coincide.

Output

For each test case, print the answer as an integer in one line.

Sample Input

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

Sample Output

3
4
2

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int x1[2],x2[2],y1[2],y2[2];
        cin>>x1[0]>>y1[0]>>x2[0]>>y2[0];
        cin>>x1[1]>>y1[1]>>x2[1]>>y2[1];
        if(x1[0]==x1[1]&&y1[0]==y1[1]&&x2[0]==x2[1]&&y2[0]==y2[1])cout<<2<<endl;
        else if(x1[1]>x1[0]&&y1[1]==y1[0]&&x2[1]<x2[0]&&y2[1]==y2[0])cout<<4<<endl;
        else if(x1[0]>x1[1]&&y1[0]==y1[1]&&x2[0]<x2[1]&&y2[0]==y2[1])cout<<4<<endl;
        else if(x1[0]==x1[1]&&y1[1]>y1[0]&&x2[0]==x2[1]&&y2[1]<y2[0])cout<<4<<endl;
        else if(x1[0]==x1[1]&&y1[0]>y1[1]&&x2[0]==x2[1]&&y2[0]<y2[1])cout<<4<<endl;
        else if(x1[1]>=x1[0]&&y1[1]>=y1[0]&&x2[1]<=x2[0]&&y2[1]<=y2[0])cout<<3<<endl;
        else if(x1[0]>=x1[1]&&y1[0]>=y1[1]&&x2[0]<=x2[1]&&y2[0]<=y2[1])cout<<3<<endl;
        else if(x1[1]>=x2[0])cout<<3<<endl;
        else if(x1[0]>=x2[1])cout<<3<<endl;
        else if(y1[1]>=y2[0])cout<<3<<endl;
        else if(y1[0]>=y2[1])cout<<3<<endl;
        else if(x1[1]==x1[0]&&y1[1]<y1[0]&&x2[1]<x2[0]&&y2[1]>y2[0])cout<<5<<endl;
        else if(x1[0]==x1[1]&&y1[0]<y1[1]&&x2[0]<x2[1]&&y2[0]>y2[1])cout<<5<<endl;
        else if(x1[1]>x1[0]&&y1[1]<y1[0]&&x2[1]==x2[0]&&y2[1]>y2[0])cout<<5<<endl;
        else if(x1[0]>x1[1]&&y1[0]<y1[1]&&x2[0]==x2[1]&&y2[0]>y2[1])cout<<5<<endl;
        else if(x1[1]>x1[0]&&y1[1]==y1[0]&&x2[1]<x2[0]&&y2[1]>y2[0])cout<<5<<endl;
        else if(x1[0]>x1[1]&&y1[0]==y1[1]&&x2[0]<x2[1]&&y2[0]>y2[1])cout<<5<<endl;
        else if(x1[1]>x1[0]&&y1[1]<y1[0]&&x2[1]<x2[0]&&y2[1]==y2[0])cout<<5<<endl;
        else if(x1[0]>x1[1]&&y1[0]<y1[1]&&x2[0]<x2[1]&&y2[0]==y2[1])cout<<5<<endl;
        else if(x1[1]>x1[0]&&y1[1]<y1[0]&&x2[1]<x2[0]&&y2[1]>y2[0])cout<<6<<endl;
        else if(x1[0]>x1[1]&&y1[0]<y1[1]&&x2[0]<x2[1]&&y2[0]>y2[1])cout<<6<<endl;
        else cout<<4<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值