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;
void ga(int N, int *A) {
  for (int i(0); i < N; i++) scanf("%d", A + i);
}
struct sX {
  long long p[(1 << 20)], P[(1 << 20)];
  int N;
  void ini(int n) { N = 1 << (int(log2(n) - (1e-10)) + 1); }
  int gt(int u) { return p[u] + P[u]; }
  void st(int b, int e, long long v) {
    if (b > e) return;
    e = min(e, N - 1);
    st(b, e, v, 1, 0, N - 1);
  }
  void st(int b, int e, long long v, int u, int B, int E) {
    if (B > e || E < b) return;
    if (b <= B && e >= E) {
      P[u] += v;
      return;
    }
    st(b, e, v, u << 1, B, (B + E) >> 1),
        st(b, e, v, u << 1 | 1, (B + E) / 2 + 1, E);
    p[u] = min(gt(u << 1), gt(u << 1 | 1));
  }
  pair<int, int> gt() { return gt(1, 0, N - 1); }
  pair<int, int> gt(int u, int B, int E) {
    if (B == E) return {gt(u), B};
    pair<int, int> a;
    if (gt(u << 1 | 1) < gt(u << 1))
      a = gt(u << 1 | 1, (B + E) / 2 + 1, E);
    else
      a = gt(u << 1, B, (B + E) >> 1);
    a.first += P[u];
    return a;
  }
} U, V;
struct FW {
  int t[(1 << 20)], S;
  void clr(int s) {
    (memset(t, 0, sizeof(t)));
    S = s;
  }
  void inc(int i, int d) {
    for (; i < S; i |= i + 1) t[i] += d;
  }
  int sum(int i) {
    int s(0);
    while (~i) s += t[i], i &= i + 1, --i;
    return s;
  }
  int gt(int l, int r) { return sum(r) - sum(l - 1); }
} F, G;
set<pair<pair<int, int>, pair<int, int> > > X, Y, t;
pair<pair<int, int>, pair<int, int> > g[(1 << 20)];
int N, A[(1 << 20)], B[(1 << 20)], a, b, c, d, I, J;
void md(int a, int A, int b, int B) {
  X.insert({{a, A}, {b, B}}), Y.insert({{b, B}, {a, A}});
  F.inc(a, 1), G.inc(b, 1);
  U.st(a, A, 1), V.st(b, B, 1);
}
void dl(int a, int A, int b, int B) {
  X.erase({{a, A}, {b, B}}), Y.erase({{b, B}, {a, A}});
  F.inc(a, -1), G.inc(b, -1);
  U.st(a, A, -1), V.st(b, B, -1);
}
void dl(set<pair<pair<int, int>, pair<int, int> > > &X) {
  for (auto &h : X)
    dl(h.first.first, h.first.second, h.second.first, h.second.second);
}
bool go() {
  if (X.size() == 1u) return t = X, dl(t), 1;
  set<pair<pair<int, int>, pair<int, int> > > x;
  int b = (*X.begin()).first.first, e = (*--X.end()).first.first;
  U.st(0, b - 1, int(1e9 + 1)), U.st(e + 1, int(1e9 + 1), int(1e9 + 1));
  pair<int, int> v = U.gt();
  U.st(0, b - 1, -int(1e9 + 1)), U.st(e + 1, int(1e9 + 1), -int(1e9 + 1));
  if (v.first) goto YY;
  if (F.sum(v.second) <= (int)X.size() / 2) {
    while ((*X.begin()).first.first < v.second) {
      pair<int, int> a = X.begin()->first, b = X.begin()->second;
      x.insert({a, b});
      dl(a.first, a.second, b.first, b.second);
    }
  } else {
    while ((*--X.end()).first.first > v.second) {
      pair<int, int> a = (--X.end())->first, b = (--X.end())->second;
      x.insert({a, b});
      dl(a.first, a.second, b.first, b.second);
    }
  }
  if (!go()) return 0;
  for (auto &h : x)
    md(h.first.first, h.first.second, h.second.first, h.second.second);
  return go();
YY:
  b = (*Y.begin()).first.first, e = (*--Y.end()).first.first;
  V.st(0, b - 1, int(1e9 + 1)), V.st(e + 1, int(1e9 + 1), int(1e9 + 1));
  v = V.gt();
  V.st(0, b - 1, -int(1e9 + 1)), V.st(e + 1, int(1e9 + 1), -int(1e9 + 1));
  if (v.first) return 0;
  if (G.sum(v.second) <= (int)X.size() / 2) {
    while ((*Y.begin()).first.first < v.second) {
      pair<int, int> b = Y.begin()->first, a = Y.begin()->second;
      x.insert({a, b});
      dl(a.first, a.second, b.first, b.second);
    }
  } else {
    while ((*--Y.end()).first.first > v.second) {
      pair<int, int> b = (--Y.end())->first, a = (--Y.end())->second;
      x.insert({a, b});
      dl(a.first, a.second, b.first, b.second);
    }
  }
  if (!go()) return 0;
  for (auto &h : x)
    md(h.first.first, h.first.second, h.second.first, h.second.second);
  return go();
}
int main(void) {
  scanf("%d", &N), F.clr((4 * N + 666)), G.clr((4 * N + 666)),
      U.ini((4 * N + 666)), V.ini((4 * N + 666));
  for (int i(0); i < N; i++)
    scanf("%d%d%d%d", &a, &b, &c, &d), a *= 2, b *= 2, c *= 2, d *= 2,
        g[i] = {{a, c - 2}, {b, d - 2}}, A[i] = a, A[N + i] = c - 1,
        A[N * 2 + i] = c - 2, B[i] = b, B[i + N] = d - 1, B[i + 2 * N] = d - 2;
  sort(A, A + 3 * N), sort(B, B + 3 * N), I = unique(A, A + 3 * N) - A,
                                          J = unique(B, B + 3 * N) - B;
  for (int i(0); i < N; i++)
    g[i] = {{(int(upper_bound(A, A + I, g[i].first.first) - A) - 1),
             (int(upper_bound(A, A + I, g[i].first.second) - A) - 1)},
            {(int(upper_bound(B, B + J, g[i].second.first) - B) - 1),
             (int(upper_bound(B, B + J, g[i].second.second) - B) - 1)}};
  for (int i(0); i < N; i++)
    md(g[i].first.first, g[i].first.second, g[i].second.first,
       g[i].second.second);
  puts(go() ? "YES" : "NO");
  return 0;
}