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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
static class Program{
	const int mod=1000000007;
	static void Main(){
		Sc sc=new Sc();
		int[] s=sc.Ia;
		int[][] a=new int[s[2]][];
		List<int[]> li=new List<int[]>();
		string ans="YES";
		bool[] b=new bool[s[2]];
		for(int i = 0;i<s[2];i++) {
			a[i]=sc.Ia;
			if((a[i][0]==0||a[i][0]==s[0]||a[i][1]==0||a[i][1]==s[1])&&(a[i][2]==0||a[i][2]==s[0]||a[i][3]==0||a[i][3]==s[1])){
				if(a[i][1]==0){li.Add(new int[]{a[i][0],i});}
				else if(a[i][0]==s[0]){li.Add(new int[]{a[i][1]+(int)2e8,i});}
				else if(a[i][1]==s[1]){li.Add(new int[]{(int)5e8-a[i][0],i});}
				else{li.Add(new int[]{(int)8e8-a[i][1],i});}
				if(a[i][3]==0){li.Add(new int[]{a[i][2],i});}
				else if(a[i][2]==s[0]){li.Add(new int[]{a[i][3]+(int)2e8,i});}
				else if(a[i][3]==s[1]){li.Add(new int[]{(int)5e8-a[i][2],i});}
				else{li.Add(new int[]{(int)8e8-a[i][3],i});}
				b[i]=true;
			}
			a[i][0]=-1;
			a[i][1]=-1;
		}
		li.Sort((u,v)=>u[0]-v[0]);
		St st1=new St(0,li.Count,true);
		St st2=new St(0,li.Count,false);
		for(int i = 0;i<li.Count;i++) {
			if(a[li[i][1]][0]==-1){a[li[i][1]][0]=i;}
			else{
				st1.Ud(a[li[i][1]][0],i);
				st2.Ud(a[li[i][1]][0],i);
				st1.Ud(i,a[li[i][1]][0]);
				st2.Ud(i,a[li[i][1]][0]);
				a[li[i][1]][1]=i;
			}
		}
		for(int i = 0;i<s[2];i++) {
			if(b[i]&&a[i][1]-a[i][0]!=1){
				if(st1.Get(a[i][0]+1,a[i][1]-1).d>a[i][1]||st2.Get(a[i][0]+1,a[i][1]-1).d<a[i][0]){ans="NO";}
			}
		}
		Console.WriteLine(ans);
	}
}
public class St{
	public class Mo{
		public int n;
		public long d;
		public Mo(long d,int n){this.d=d;this.n=n;}
	}
	public Mo[] dat;
	private int m=1,f;
	private bool mm;
	private Mo du;
	private Func<Mo,Mo,bool> compare;
	public St(int f,int n,bool mm){
		this.f=f;
		n+=f^1;
		this.mm=mm;
		if(mm){du=new Mo(long.MinValue,-1);compare=CoMax;}
		else{du=new Mo(long.MaxValue,-1);compare=CoMin;}
		while(m<n){m*=2;}
		dat=new Mo[m+n-1];
		for(int i=0;i<m+n-1;i++){dat[i]=du;}
	}
	private bool CoMax(Mo x,Mo y){return x.d>y.d;}
	private bool CoMin(Mo x,Mo y){return x.d<y.d;}
	public void Ud(int q,long b){
		q+=m-1-f;
		if(dat[q]==du){
			int z=q;
			dat[q]=new Mo(b,q-(m-1-f));
			while(q>0&&compare(dat[z],dat[(q-1)/2])){
				dat[(q-1)/2]=dat[z];
				q=(q-1)/2;
			}
		}
		else{
			b+=dat[q].d;
			dat[q].d=du.d;
			dat[q]=new Mo(b,q-(m-1-f));
			q=(q-1)/2;
			while(q>=0){
				Mo o=compare(dat[2*q+1],dat[2*q+2])?dat[2*q+1]:dat[2*q+2];
				if(dat[q]==o){break;}
				dat[q]=o;
				q=(q-1)/2;
			}
		}
	}
	private Mo Fdg(int a,int b,int k,int l,int r){
		if(r<a||b<l){return du;}
		if(a<=l&&r<=b){return dat[k];}
		Mo p=Fdg(a,b,k*2+1,l,(l+r)/2);
		Mo q=Fdg(a,b,k*2+2,(l+r+1)/2,r);
		return compare(p,q)?p:q;
	}
	public Mo Get(int a,int b){return Fdg(a,b,0,f,m-(f^1));}
}

public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()).Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()).Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
}