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;
const int mxN = 1e5, mxC = 2 * mxN;
int n, a[mxN], b[mxN], c[mxN], d[mxN];
int x[mxC], y[mxC], xs, ys;
struct st {
  array<int, 2> st[1 << 19];
  int lz[1 << 19];
  void bld(int i = 1, int l = 0, int r = mxC - 1) {
    if (l == r) {
      st[i] = {0, l};
      return;
    }
    int m = (l + r) / 2;
    bld(2 * i, l, m);
    bld(2 * i + 1, m + 1, r);
    st[i] = st[2 * i];
  }
  void app(int i, int x) {
    st[i][0] += x;
    lz[i] += x;
  }
  void psh(int i) {
    app(2 * i, lz[i]);
    app(2 * i + 1, lz[i]);
    lz[i] = 0;
  }
  void upd(int l1, int r1, int x, int i = 1, int l2 = 0, int r2 = mxC - 1) {
    if (l1 > r1) return;
    if (l1 <= l2 && r2 <= r1) {
      app(i, x);
      return;
    }
    int m2 = (l2 + r2) / 2;
    psh(i);
    if (l1 <= m2) upd(l1, r1, x, 2 * i, l2, m2);
    if (m2 < r1) upd(l1, r1, x, 2 * i + 1, m2 + 1, r2);
    st[i] = min(st[2 * i], st[2 * i + 1]);
  }
  array<int, 2> qry(int l1, int r1, int i = 1, int l2 = 0, int r2 = mxC - 1) {
    if (l1 > r1) return {mxN};
    if (l1 <= l2 && r2 <= r1) return st[i];
    int m2 = (l2 + r2) / 2;
    psh(i);
    return min(
        l1 <= m2 ? qry(l1, r1, 2 * i, l2, m2) : array<int, 2>{mxN},
        m2 < r1 ? qry(l1, r1, 2 * i + 1, m2 + 1, r2) : array<int, 2>{mxN});
  }
} stx, sty;
struct ft {
  int ft[mxC + 1];
  void upd(int i, int x) {
    for (++i; i <= mxC; i += i & -i) ft[i] += x;
  }
  int qry(int i) {
    int r = 0;
    for (; i; i -= i & -i) r += ft[i];
    return r;
  }
} ftx, fty;
void rem(int i) {
  stx.upd(a[i] + 1, c[i] - 1, -1);
  sty.upd(b[i] + 1, d[i] - 1, -1);
  ftx.upd(a[i], -1);
  fty.upd(b[i], -1);
}
bool dc(vector<int> &v) {
  set<array<int, 2>> sx, sy, sx2, sy2;
  for (int i : v) {
    stx.upd(a[i] + 1, c[i] - 1, 1);
    sty.upd(b[i] + 1, d[i] - 1, 1);
    ftx.upd(a[i], 1);
    fty.upd(b[i], 1);
    sx.insert({a[i], i});
    sy.insert({b[i], i});
    sx2.insert({c[i], i});
    sy2.insert({d[i], i});
  }
  vector<vector<int>> vl;
  while (sx.size() > 3) {
    int ss = sx.size();
    vl.push_back({});
    array<int, 2> px = stx.qry((*sx.begin())[0] + 1, (*--sx2.end())[0] - 1);
    if (!px[0]) {
      if (ftx.qry(px[1]) > ss / 2) {
        auto it = --sx.end();
        while ((*it)[0] >= px[1]) {
          int i = (*it)[1];
          vl.back().push_back(i);
          rem(i);
          it = --sx.erase(it);
          sy.erase({b[i], i});
          sx2.erase({c[i], i});
          sy2.erase({d[i], i});
        }
      } else {
        auto it = sx.begin();
        while ((*it)[0] < px[1]) {
          int i = (*it)[1];
          vl.back().push_back(i);
          rem(i);
          it = sx.erase(it);
          sy.erase({b[i], i});
          sx2.erase({c[i], i});
          sy2.erase({d[i], i});
        }
      }
      continue;
    }
    array<int, 2> py = sty.qry((*sy.begin())[0] + 1, (*--sy2.end())[0] - 1);
    if (!py[0]) {
      if (fty.qry(py[1]) > ss / 2) {
        auto it = --sy.end();
        while ((*it)[0] >= py[1]) {
          int i = (*it)[1];
          vl.back().push_back(i);
          rem(i);
          sx.erase({a[i], i});
          it = --sy.erase(it);
          sx2.erase({c[i], i});
          sy2.erase({d[i], i});
        }
      } else {
        auto it = sy.begin();
        while ((*it)[0] < py[1]) {
          int i = (*it)[1];
          vl.back().push_back(i);
          rem(i);
          sx.erase({a[i], i});
          it = sy.erase(it);
          sx2.erase({c[i], i});
          sy2.erase({d[i], i});
        }
      }
      continue;
    }
    return 0;
  }
  for (array<int, 2> a : sx) rem(a[1]);
  for (vector<int> &vi : vl)
    if (!dc(vi)) return 0;
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i] >> b[i] >> c[i] >> d[i];
  memcpy(x, a, 4 * n);
  memcpy(x + n, c, 4 * n);
  sort(x, x + 2 * n);
  xs = unique(x, x + 2 * n) - x;
  stx.bld();
  memcpy(y, b, 4 * n);
  memcpy(y + n, d, 4 * n);
  sort(y, y + 2 * n);
  ys = unique(y, y + 2 * n) - y;
  sty.bld();
  for (int i = 0; i < n; ++i) {
    a[i] = lower_bound(x, x + xs, a[i]) - x;
    c[i] = lower_bound(x, x + xs, c[i]) - x;
    b[i] = lower_bound(y, y + ys, b[i]) - y;
    d[i] = lower_bound(y, y + ys, d[i]) - y;
  }
  vector<int> v(n);
  iota(v.begin(), v.end(), 0);
  cout << (dc(v) ? "YES" : "NO");
}