Problem: This problem differs from the previous problem only in constraints.

Petya decided to visit Byteland during the summer holidays. It turned out that the history of this country is quite unusual.

Initially, there were n different countries on the land that is now Berland. Each country had its own territory that was represented as a rectangle on the map. The sides of the rectangle were parallel to the axes, and the corners were located at points with integer coordinates. Territories of no two countries intersected, but it was possible that some territories touched each other. As time passed, sometimes two countries merged into one. It only happened if the union of their territories was also a rectangle. In the end only one country remained — Byteland.

Initially, each country had a rectangular castle inside its territory. Its sides were parallel to the axes and its corners had integer coordinates. Some castles might touch the border of the corresponding country and sides or other castles. Miraculously, after all the unions the castles are still intact. Unfortunately, their locations are the only information we have to restore the initial territories of the countries.

<image> The possible formation of Byteland. The castles are shown in blue. 

Petya wonders why no information about the initial countries remained. He suspected that the whole story is a fake. You were recommended to him as a smart person. Please check whether or not there exists a possible set of initial territories that could make the story true.

Input

The first line contains a single integer n (1 ≤ n ≤ 100 000) — the number of countries and castles.

Each of the next n lines contains four integers a_i, b_i, c_i, d_i (0 ≤ a_i < c_i ≤ 10^9, 0 ≤ b_i < d_i ≤ 10^9) — the coordinates of the i-th castle, where (a_i, b_i) are the coordinates of the lower left corner and (c_i, d_i) are the coordinates of the upper right corner.

It is guaranteed that no two castles intersect, however, they may touch.

Output

If there exists a possible set of territories that satisfies the story, print "YES", otherwise print "NO".

You can print each letter in any case (upper or lower).

Examples

Input


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


Output


YES


Input


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


Output


NO

Note

The castles in the first and second examples are shown on the pictures below. 

<image> <image>

Solution (in JAVA):

Solution:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class StoryOfCountry {	
	public static void main(String[] args) {
		FS in = new FS();
		int N = in.nextInt();
		TreeSet<Rec> hEnd = HE();
		TreeSet<Rec> hStart = HS();
		TreeSet<Rec> vEnd = VE();
		TreeSet<Rec> vStart = VS();
		for(int i = 0; i < N; i++) {
			Rec r = new Rec(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), i);
			hEnd.add(r); hStart.add(r); vEnd.add(r); vStart.add(r);
		}
		System.out.println(solve(0, hEnd, hStart, vEnd, vStart) ? "YES":"NO");
	}
	
	/* State:
	 *  0 -> can be either vertical or horizontal
	 *  1 -> has to be vertical split
	 *  2 -> has to be horizontal split
	 */
	static boolean solve(int state, TreeSet<Rec> he, TreeSet<Rec> hs, TreeSet<Rec> ve, TreeSet<Rec> vs) {
		if(he.size() <= 2) return true; // Always possible with just 2 things
		
		TreeSet<Rec> lhe = HE();
		TreeSet<Rec> lhs = HS();
		TreeSet<Rec> lve = VE();
		TreeSet<Rec> lvs = VS();
		
		TreeSet<Rec> rhe = HE();
		TreeSet<Rec> rhs = HS();
		TreeSet<Rec> rve = VE();
		TreeSet<Rec> rvs = VS();
		
		TreeSet<Rec> lhe2 = HE();
		TreeSet<Rec> lhs2 = HS();
		TreeSet<Rec> lve2 = VE();
		TreeSet<Rec> lvs2 = VS();
		
		TreeSet<Rec> rhe2 = HE();
		TreeSet<Rec> rhs2 = HS();
		TreeSet<Rec> rve2 = VE();
		TreeSet<Rec> rvs2 = VS();
		
		Rec lR = null;
		Rec lRS = null;
		int maxLRS = -1;
		Rec rR = null;
		Rec rRS = null;
		int minRRS = 2131231231;
		
		Rec lR2 = null;
		Rec lRS2 = null;
		int maxLRS2 = -1;
		Rec rR2 = null;
		Rec rRS2 = null;
		int minRRS2 = 2131231231;
			
			
		while((state != 2 && (lR == null || rR == null || lR.x2 < rR.x1)) || (state != 1 && (lR2 == null || rR2 == null || lR2.y2 < rR2.y1))) {
			//do left side
			if(state != 2 && (lR == null || rR == null || lR.x2 < rR.x1)) { 
				lR = (lR == null ? he.first() : he.higher(lR));
				lhe.add(lR);
				while(true) {
					Rec next = (lRS == null ? hs.first() : hs.higher(lRS));
					if(next == null || next.x1 >= lR.x2) break;
					maxLRS = Math.max(maxLRS, next.x2);
					lRS = next;
				}
				if(lR.x2 >= maxLRS) { //found a working thing
					Rec r;
					while((r = he.higher(lR)) != null && r.x2 == lR.x2){ lhe.add(r); lR = r; }
						for(Rec rr : lhe) {
							he.remove(rr); hs.remove(rr); ve.remove(rr); vs.remove(rr);
							lhs.add(rr); lve.add(rr); lvs.add(rr);
						}
					return solve(2,lhe,lhs,lve,lvs)&&solve(0,he,hs,ve,vs);
				}
				
				//do right side
				rR = (rR == null ? hs.last() : hs.lower(rR));
				rhe.add(rR);
				while(true) {
					Rec next = (rRS == null ? he.last() : he.lower(rRS));
					if(next == null || next.x2 <= rR.x1) break;
					minRRS = Math.min(minRRS, next.x1);
					rRS = next;
				}
				if(rR.x1 <= minRRS) { //found a working thing
					Rec r;
					while((r = hs.lower(rR)) != null && r.x1 == rR.x1){ rhe.add(r); rR = r; }
						for(Rec rr : rhe) {
							he.remove(rr); hs.remove(rr); ve.remove(rr); vs.remove(rr);
							rhs.add(rr); rve.add(rr); rvs.add(rr);
						}
					return solve(0,he,hs,ve,vs)&&solve(2,rhe,rhs,rve,rvs);
				}
			}
			
			if(state != 1 && (lR2 == null || rR2 == null || lR2.y2 < rR2.y1)) {
				//do left side2
				lR2 = (lR2 == null ? ve.first() : ve.higher(lR2));
				lhe2.add(lR2);
				while(true) {
					Rec next = (lRS2 == null ? vs.first() : vs.higher(lRS2));
					if(next == null || next.y1 >= lR2.y2) break;
					maxLRS2 = Math.max(maxLRS2, next.y2);
					lRS2 = next;
				}
				if(lR2.y2 >= maxLRS2) { //found a working thing
					Rec r;
					while((r = ve.higher(lR2)) != null && r.y2 == lR2.y2){ lhe2.add(r); lR2 = r; }
						for(Rec rr : lhe2) {
							he.remove(rr); hs.remove(rr); ve.remove(rr); vs.remove(rr);
							lhs2.add(rr); lve2.add(rr); lvs2.add(rr);
						}
					return solve(1,lhe2,lhs2,lve2,lvs2)&&solve(0,he,hs,ve,vs);
				}
				
				//do right side2
				rR2 = (rR2 == null ? vs.last() : vs.lower(rR2));
				rhe2.add(rR2);
				while(true) {
					Rec next = (rRS2 == null ? ve.last() : ve.lower(rRS2));
					if(next == null || next.y2 <= rR2.y1) break;
					minRRS2 = Math.min(minRRS2, next.y1);
					rRS2 = next;
				}
				if(rR2.y1 <= minRRS2) { //found a working thing
					Rec r;
					while((r = vs.lower(rR2)) != null && r.y1 == rR2.y1){ rhe2.add(r); rR2 = r; }
						for(Rec rr : rhe2) {
							he.remove(rr); hs.remove(rr); ve.remove(rr); vs.remove(rr);
							rhs2.add(rr); rve2.add(rr); rvs2.add(rr);
						}
					return solve(0,he,hs,ve,vs)&&solve(1,rhe2,rhs2,rve2,rvs2);
				}
			}
		}	
		return false;
	}
	
	static TreeSet<Rec> HE() {
		return new TreeSet<Rec>((a,b) -> (a.x2 == b.x2 ? a.id-b.id : a.x2-b.x2));
	}
	static TreeSet<Rec> HS(){
		return new TreeSet<Rec>((a,b) -> (a.x1 == b.x1 ? a.id-b.id : a.x1-b.x1));		
	}
	static TreeSet<Rec> VE(){
		return new TreeSet<Rec>((a,b) -> (a.y2 == b.y2 ? a.id-b.id : a.y2-b.y2));
	}
	static TreeSet<Rec> VS(){
		return new TreeSet<Rec>((a,b) -> (a.y1 == b.y1 ? a.id-b.id : a.y1-b.y1));		
	}
	static class Rec{
		int x1,y1,x2,y2,id;
		public Rec(int a, int b, int c, int d, int ii) {
			x1=a; y1=b; x2=c; y2=d;
			id=ii;
		}
	}

	static class FS{
		BufferedReader br;
		StringTokenizer st;
		public FS() {
			br = new BufferedReader(new InputStreamReader(System.in));	
		}
		String next() {
			while(st == null || !st.hasMoreElements()) {
				try { st = new StringTokenizer(br.readLine());}
				catch(Exception e) { throw null;}
			}
			return st.nextToken();
		}
		int nextInt() { return Integer.parseInt(next());}
	}
}