【思维】C. Rectangles
【思维】C. Rectangles
题意:在n个矩形中取n-1个矩形来对其所在的区域取交集,如果最终所形成的区域仍是存在的,就输出这个区域内任意一个点的坐标
思路:提前处理好前i个矩形的交集情况和后i个矩形的交集情况,然后从1到n枚举一个点,把这个点两边的交集情况再做一次合取(取交集),判断一下是否符合题意。
#include <bits/stdc++.h>
using namespace std;
int n,m;
const int N = 2E+5;
struct res{
int x,y,xx,yy;
}ress[N],suff[N],pre[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>ress[i].x>>ress[i].y>>ress[i].xx>>ress[i].yy;
pre[1].x=ress[1].x;pre[1].y=ress[1].y;
pre[1].xx=ress[1].xx;pre[1].yy=ress[1].yy;
for(int i=2;i<=n;i++)
{
pre[i].x=max(pre[i-1].x,ress[i].x);
pre[i].y=max(pre[i-1].y,ress[i].y);
pre[i].xx=min(pre[i-1].xx,ress[i].xx);
pre[i].yy=min(pre[i-1].yy,ress[i].yy);
}
if(pre[n-1].x<=pre[n-1].xx&&pre[n-1].y<=pre[n-1].yy)
cout<<pre[n-1].x<<" "<<pre[n-1].y;
else
{
suff[n].x=ress[n].x;suff[n].y=ress[n].y;
suff[n].xx=ress[n].xx;suff[n].yy=ress[n].yy;
for(int i=n-1;i>=2;i--)
{
suff[i].x=max(suff[i+1].x,ress[i].x);
suff[i].y=max(suff[i+1].y,ress[i].y);
suff[i].xx=min(suff[i+1].xx,ress[i].xx);
suff[i].yy=min(suff[i+1].yy,ress[i].yy);
res test;
test.x=max(suff[i+1].x,pre[i-1].x);
test.y=max(suff[i+1].y,pre[i-1].y);
test.xx=min(suff[i+1].xx,pre[i-1].xx);
test.yy=min(suff[i+1].yy,pre[i-1].yy);
if(test.x<=test.xx&&test.y<=test.yy)
{
cout<<test.x<<" "<<test.y;
return 0;
}
}
if(suff[2].x<=suff[2].xx&&suff[2].y<=suff[2].yy)
cout<<suff[2].x<<" "<<suff[2].y;
}
return 0;
}