AtCoder Regular Contest 076 E - Connected?
题目传送门:https://arc076.contest.atcoder.jp/tasks/arc076_c
题目大意:
给定一个\(R×C\)的矩阵,然后给定\(N\)对点,每对点坐标为\((X_{i,1},Y_{i,1})\)和\((X_{i,2},Y_{i,2})\),每对点之间需要连一条线,线不能越出矩阵边界,也不能相交,问是否可能?
只有一对点都在矩阵边缘上,才可能截断其他点对的连线,那么我们从任意一个地方断开矩阵,将其展开为一条线段,那么点对相当于覆盖了线段上的一段区间,于是题目变成了区间判交问题
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Fi first
#define Se second
#define MK make_pair
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=1e5;
int R,C,n,Mpcnt,tot,top;
pii A[(N<<1)+10];
bool check(int x,int y){return x==0||x==R||y==0||y==C;}
int stack[(N<<1)+10];
int get(int x,int y){
if (y==0) return x;
if (x==R) return R+y;
if (y==C) return 2*R+C-x;
if (x==0) return 2*R+2*C-y;
return 0;
}
int main(){
R=read(),C=read(),n=read();
for (int i=1;i<=n;i++){
int x1=read(),y1=read(),x2=read(),y2=read();
if (!check(x1,y1)||!check(x2,y2)) continue;
int x=get(x1,y1),y=get(x2,y2);
A[++tot]=MK(x,i);
A[++tot]=MK(y,i);
}
sort(A+1,A+1+tot);
for (int i=1;i<=tot;i++){
stack[++top]=i;
if (A[stack[top]].Se==A[stack[top-1]].Se) top-=2;
}
printf(top?"NO\n":"YES\n");
return 0;
}