Problem:
Score : 700 points

Problem Statement

Snuke is playing a puzzle game.
In this game, you are given a rectangular board of dimensions R × C, filled with numbers. Each integer i from 1 through N is written twice, at the coordinates (x_{i,1},y_{i,1}) and (x_{i,2},y_{i,2}).

The objective is to draw a curve connecting the pair of points where the same integer is written, for every integer from 1 through N.
Here, the curves may not go outside the board or cross each other.

Determine whether this is possible.

Constraints

1 ≤ R,C ≤ 10^8

1 ≤ N ≤ 10^5

0 ≤ x_{i,1},x_{i,2} ≤ R(1 ≤ i ≤ N)

0 ≤ y_{i,1},y_{i,2} ≤ C(1 ≤ i ≤ N)

All given points are distinct.

All input values are integers.

Input

Input is given from Standard Input in the following format:

R C N
x_{1,1} y_{1,1} x_{1,2} y_{1,2}
:
x_{N,1} y_{N,1} x_{N,2} y_{N,2}

Output

Print YES if the objective is achievable; print NO otherwise.

Sample Input 1

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

Sample Output 1

YES

The above figure shows a possible solution.

Sample Input 2

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

Sample Output 2

NO

Sample Input 3

5 5 7
0 0 2 4
2 3 4 5
3 5 5 2
5 5 5 4
0 3 5 1
2 2 4 4
0 5 4 1

Sample Output 3

YES

Sample Input 4

1 1 2
0 0 1 1
1 0 0 1

Sample Output 4

NO

Solution in C++ (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
#define reg register
#define ll long long
inline ll read()
{
	ll x=0,w=0;char ch=getchar();
	while(!isdigit(ch))w|=ch=='-',ch=getchar();
	while(isdigit(ch))x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return w?-x:x;
}
struct op{int x,y,a,b;}B[400005];
int R,C,n,tot;
inline bool com(op x,op y){return (x.x==y.x)?(x.y<y.y):(x.x<y.x);}
struct BIT
{
    int szsz[400005];
    inline void modify(int x)
    {
        for(reg int i=x+1;i<=400000;i+=i&(-i))szsz[i]++;
    }
    inline int ask(int x)
    {
        reg int res=0;
        for(reg int i=x+1;i;i^=i&(-i))res+=szsz[i];
        return res;
    }
    inline int query(int l,int r)
    {return ask(r)-ask(l-1);}
}xi,xa,yi,ya;
#define No {puts("NO");exit(0);}
inline void bd(int x,int y,int a,int b)
{
    if(x==a&&x==0){if(xi.query(y,b))No;return ;}
    if(x==a&&x==R){if(xa.query(y,b))No;return ;}
    if(y==b&&y==0){if(yi.query(x,a))No;return ;}
    if(y==b&&y==C){if(ya.query(x,a))No;return ;}
    if(x==0&&b==C)
    {
        if(ya.query(0,a))No;
        if(xi.query(y,C))No;return ;
    }
    if(x==0&&b==0)
    {
        if(xi.query(y,C))No;
        if(ya.query(0,R))No;
        if(xa.query(0,C))No;
        if(yi.query(a,R))No;return ;
    }
    if(a==R&&y==C)
    {
        if(xa.query(b,C))No;
        if(ya.query(x,R))No;return ;
    }
    if(a==R&&y==0)
    {
        if(xa.query(0,b))No;
        if(yi.query(x,R))No;return ;
    }
    if((y==0||y==C)&&(b==0||b==C))
    {
        if(xa.query(0,C))No;
        if(y==0)
        {
            if(yi.query(x,R))No;
            if(ya.query(a,R))No;
        }
        else
        {
            if(yi.query(a,R))No;
            if(ya.query(x,R))No;
        }return ;
    }
    if(ya.query(0,R))No;
    if(xi.query(y,C))No;
    if(xa.query(b,C))No;
}
int gs1,gs2,ax[400010],by[400010];
int xx[400010],yy[400010],aa[400010],bb[400010];
int main()
{
    R=read(),C=read(),n=read();
    reg int i,x,y,a,b;
    for(i=1;i<=n;++i)
    {
        xx[i]=read(),yy[i]=read();aa[i]=read(),bb[i]=read();
        ax[++gs1]=xx[i];ax[++gs1]=aa[i];
        by[++gs2]=yy[i];by[++gs2]=bb[i];
    }
    ax[++gs1]=0;ax[++gs1]=R;by[++gs2]=0;by[++gs2]=C;
    sort(ax+1,ax+1+gs1);sort(by+1,by+1+gs2);
    gs1=unique(ax+1,ax+1+gs1)-ax-1;
    gs2=unique(by+1,by+1+gs2)-by-1;
    for(i=1;i<=n;++i)
    {
        xx[i]=lower_bound(ax+1,ax+1+gs1,xx[i])-ax-1;
        yy[i]=lower_bound(by+1,by+1+gs2,yy[i])-by-1;
        aa[i]=lower_bound(ax+1,ax+1+gs1,aa[i])-ax-1;
        bb[i]=lower_bound(by+1,by+1+gs2,bb[i])-by-1;
    }
    R=lower_bound(ax+1,ax+1+gs1,R)-ax-1;
    C=lower_bound(by+1,by+1+gs2,C)-by-1;
    for(i=1;i<=n;++i)
    {
        x=xx[i],y=yy[i],a=aa[i],b=bb[i];
        if(x!=0&&x!=R&&y!=0&&y!=C)continue;
        if(a!=0&&a!=R&&b!=0&&b!=C)continue;
        if(x>a){swap(x,a),swap(y,b);}
        else{if(x==a&&y>b)swap(y,b);}
        B[++tot]=(op){x,y,a,b};
    }sort(B+1,B+1+tot,com);
    for(i=1;i<=tot;++i)
    {
        x=B[i].x;y=B[i].y;a=B[i].a;b=B[i].b;
        bd(x,y,a,b);
        if(a==0)xi.modify(b);
        if(a==R)xa.modify(b);
        if(b==0)yi.modify(a);
        if(b==C)ya.modify(a);
    }puts("YES");
	return 0;
}