Language: C++

#include <iostream>
#include <vector>
using namespace std;
const int NICO = 100000+10;
int r, c, n;
long long x1[NICO], x2[NICO], y1[NICO], y2[NICO];
vector<int> v1, v2;

#define cs const
#define cp const P&
#define op operator
const  double eps = 1e-8;
inline int sig(double x) {return (x>eps)-(x<-eps);}

struct P{
    double x, y;
    void in() { scanf("%lf%lf", &x, &y); }
    P(double x=0.0, double y=0.0) : x(x), y(y) {}

    P op-(cp a)cs { return P(x-a.x, y-a.y); }
    double op^(cp a)cs { return x*a.y - y*a.x; }    //叉积
    double op*(cp a)cs {return x*a.x + y*a.y;}

    double cross(P a, P b) { return (a-*this) ^ (b-*this); }
    double dot(P a, P b)  { return (a-(*this)) * (b-(*this)); }
    bool on_seg(P a, P b) { return !sig(cross(a, b)) && sig(dot(a, b)) <= 0; }//判断是否在点上
};

bool seg(P a, P b, P c, P d) { //判断相交(a - b)线段 、(c - d)线段
    if(a.on_seg(c, d) || b.on_seg(c, d) || c.on_seg(a, b) || d.on_seg(a, b))
        return true;
    return sig(a.cross(b, c)*a.cross(b, d)) < 0 && sig(c.cross(d, a)*c.cross(d, b)) < 0;
}


bool ok(int i, int j) {
    P a, b, c, d;
    a.x = x1[i], a.y = y1[i];
    b.x = x2[i], b.y = y2[i];
    c.x = x1[j], c.y = y1[j];
    d.x = x2[j], d.y = y2[j];
    //cout << a.x << " " << a.y << " " << b.x << " " << b.y << " " << 
    //c.x << " " << c.y << " " << d.x << " " << d.y << endl;
    return !seg(a, b, c, d);
}

bool cmp1(int i, int j) {
    if(x1[i] == x1[j]) {
        return y1[i] > y2[j];
    }
    return x1[i] < x2[j];
}

bool cmp2(int i, int j) {
    if(y1[i] == y1[j]) {
        return x1[i] < x1[j];
    }
    return y1[i] < y1[j];
}

int main() {
    int gg = 0;
    scanf("%d %d %d", &r, &c, &n);
    for(int i=1;i<=n;i++) {
        scanf("%lld %lld %lld %lld", &x1[i], &y1[i], &x2[i], &y2[i]);
        if((x2[i]-x1[i]) * (y2[i]-y1[i]) >= 0) {
            if(x1[i] > x2[i]) {
                swap(x1[i], x2[i]);
                swap(y1[i], y2[i]);
            }
            if(y1[i] > y2[i]) {
                swap(x1[i], x2[i]);
                swap(y1[i], y2[i]);
            }

            if((x1[i] == 0 || y1[i] == 0) && (x2[i] == r || y2[i] == c)) {
                v1.push_back(i);
            }
        }

        if((x2[i]-x1[i]) * (y2[i]-y1[i]) < 0) {
            if(x1[i] > x2[i]) {
                swap(x1[i], x2[i]);
                swap(y1[i], y2[i]);
            }

            if((x1[i] == 0 || y1[i] == c) && (x2[i] == r || y2[i] == 0)) {
                //cout << x1[i] << " " << y1[i] << " " << x2[i] << " " << y2[i] << endl;
                v2.push_back(i);
            }

        }
    }

    //cout << v1.size() << " " << v2.size() << endl;
    sort(v1.begin(), v1.end(), cmp1);
    for(int i=0;i<(int)v1.size()-1;i++) {
        if(!ok(v1[i], v1[i+1])) {
            gg = 1;
        }
    }
    sort(v2.begin(), v2.end(), cmp2);
    for(int i=0;i<(int)v2.size()-1;i++) {
        if(!ok(v2[i], v2[i+1])) {
            gg = 1;
        }
    }    
    if(v1.size() > 0 && v2.size() > 0) {
        if(!ok(v1[0], v2[0])) gg = 1;
        if(!ok(v1[0], v2[(int)v2.size()-1])) gg = 1;
        if(!ok(v1[(int)v1.size()-1], v2[0])) gg = 1;
        if(!ok(v1[(int)v1.size()-1], v2[v2.size()-1])) gg = 1;
    }
    printf("%s\n", gg?"NO":"YES");
}

Compiler output:
a.cc:6:31: warning: built-in function 'y1' declared as non-function [-Wbuiltin-declaration-mismatch]
    6 | long long x1[NICO], x2[NICO], y1[NICO], y2[NICO];
      |                               ^~
a.cc: In function 'int main()':
a.cc:96:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   96 |     sort(v1.begin(), v1.end(), cmp1);
      |     ^~~~
      |     short