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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
struct rect {
  int x1, y1, x2, y2;
};
rect P[101010];
struct cmp1 {
  bool operator()(int &ca, int &cb) { return P[ca].x1 > P[cb].x1; }
};
struct cmp2 {
  bool operator()(int &ca, int &cb) { return P[ca].x2 < P[cb].x2; }
};
struct cmp3 {
  bool operator()(int &ca, int &cb) { return P[ca].y1 > P[cb].y1; }
};
struct cmp4 {
  bool operator()(int &ca, int &cb) { return P[ca].y2 < P[cb].y2; }
};
int chk[101010];
int n, cnt;
bool f(vector<int> &V) {
  int x = ++cnt;
  for (int &t : V) {
    chk[t] = x;
  }
  if (V.size() == 1) return 1;
  vector<int> X1, X2, X3, X4;
  priority_queue<int, vector<int>, cmp1> Q1;
  priority_queue<int, vector<int>, cmp2> Q2;
  priority_queue<int, vector<int>, cmp3> Q3;
  priority_queue<int, vector<int>, cmp4> Q4;
  int lx, rx, ly, ry, k, b;
  for (int &t : V) {
    Q1.push(t);
    Q2.push(t);
    Q3.push(t);
    Q4.push(t);
  }
  for (;;) {
    if (Q1.empty() || Q2.empty() || Q3.empty() || Q4.empty()) return 1;
    X1.clear();
    X2.clear();
    X3.clear();
    X4.clear();
    lx = ly = 1e9;
    rx = ry = 0;
    for (;;) {
      for (; !Q1.empty() && chk[Q1.top()] > x; Q1.pop())
        ;
      for (; !Q2.empty() && chk[Q2.top()] > x; Q2.pop())
        ;
      for (; !Q3.empty() && chk[Q3.top()] > x; Q3.pop())
        ;
      for (; !Q4.empty() && chk[Q4.top()] > x; Q4.pop())
        ;
      if (Q1.empty()) {
        b = 1;
        break;
      }
      k = Q1.top();
      if (P[k].x1 >= rx && !X1.empty()) {
        b = 1;
        break;
      }
      X1.push_back(k);
      rx = max(rx, P[k].x2);
      Q1.pop();
      if (Q2.empty()) {
        b = 2;
        break;
      }
      k = Q2.top();
      if (P[k].x2 <= lx && !X2.empty()) {
        b = 2;
        break;
      }
      X2.push_back(k);
      lx = min(lx, P[k].x1);
      Q2.pop();
      if (Q3.empty()) {
        b = 3;
        break;
      }
      k = Q3.top();
      if (P[k].y1 >= ry && !X3.empty()) {
        b = 3;
        break;
      }
      X3.push_back(k);
      ry = max(ry, P[k].y2);
      Q3.pop();
      if (Q4.empty()) {
        b = 4;
        break;
      }
      k = Q4.top();
      if (P[k].y2 <= ly && !X4.empty()) {
        b = 4;
        break;
      }
      X4.push_back(k);
      ly = min(ly, P[k].y1);
      Q4.pop();
      if (X1.size() * 2 > V.size()) return 0;
    }
    if (b == 1 && !f(X1)) return 0;
    if (b == 2 && !f(X2)) return 0;
    if (b == 3 && !f(X3)) return 0;
    if (b == 4 && !f(X4)) return 0;
    if (b != 1)
      for (int &t : X1)
        if (chk[t] <= x) Q1.push(t);
    if (b != 2)
      for (int &t : X2)
        if (chk[t] <= x) Q2.push(t);
    if (b != 3)
      for (int &t : X3)
        if (chk[t] <= x) Q3.push(t);
    if (b != 4)
      for (int &t : X4)
        if (chk[t] <= x) Q4.push(t);
  }
  return 1;
}
int main() {
  vector<int> V;
  int i;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d%d%d%d", &P[i].x1, &P[i].y1, &P[i].x2, &P[i].y2);
    V.push_back(i);
  }
  printf("%s\n", f(V) ? "YES" : "NO");
  return 0;
}