Solution
脑补一下有端点不在边界上的点显然是没有影响的。
那就是要处理剩下的点。
将剩下的点按极角排序以后,如果两端点覆盖的区间有交且不存在包含关系时,必然不合法。
这个东西维护一个栈就好了。
#include <bits/stdc++.h>
using namespace std;
const int N = 101010;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
template<typename T>
inline void read(T &x) {
static char c; x = 0; int sgn = 0;
for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
if (sgn) x = -x;
}
int ans;
int r, c, n, pnt, top;
int sta[N], vis[N];
struct Point {
int x, y, id;
};
Point a[N << 1];
inline int type(Point a) {
if (a.y == c) return 0;
if (a.x == r) return 1;
if (a.y == 0) return 2;
if (a.x == 0) return 3;
return 4;
}
inline bool cmp(Point a, Point b) {
int ta = type(a), tb = type(b);
if (ta != tb) return ta < tb;
if (ta == 0) return a.x < b.x;
if (ta == 1) return a.y > b.y;
if (ta == 2) return a.x > b.x;
if (ta == 3) return a.y < b.y;
}
int main(void) {
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
read(r); read(c); read(n);
ans = 1;
for (int i = 1; i <= n; i++) {
read(a[++pnt].x); read(a[pnt].y); a[pnt].id = i;
read(a[++pnt].x); read(a[pnt].y); a[pnt].id = i;
if (type(a[pnt]) == 4 || type(a[pnt - 1]) == 4) pnt -= 2;
}
sort(a + 1, a + pnt + 1, cmp);
for (int i = 1; i <= pnt; i++) {
if (vis[a[i].id]) {
if (sta[top] != a[i].id) ans = 0;
--top;
} else {
vis[a[i].id] = 1; sta[++top] = a[i].id;
}
}
if (ans) printf("YES");
else printf("NO");
return 0;
}