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;
long long mod = 1000000007LL;
long long large = 2000000000000000000LL;
vector<int> a, b, c, g;
int n;
bool run_small(vector<int> t);
bool run_large(map<int, int> &l, map<int, int> &r, map<int, int> &u,
               map<int, int> &d, int ls, int rs, int us, int ds, set<int> &del);
bool run_small(vector<int> t) {
  assert(t.size());
  if (t.size() == 1) return true;
  vector<pair<int, int> > fl, fr, fu, fd;
  for (int i = 0; i < (int)t.size(); i++) {
    int id = t[i];
    fl.push_back(pair<int, int>(a[id], id));
    fr.push_back(pair<int, int>(c[id], id));
    fd.push_back(pair<int, int>(b[id], id));
    fu.push_back(pair<int, int>(g[id], id));
  }
  sort(fl.begin(), fl.end());
  sort(fr.begin(), fr.end());
  sort(fd.begin(), fd.end());
  sort(fu.begin(), fu.end());
  reverse(fr.begin(), fr.end());
  reverse(fu.begin(), fu.end());
  map<int, int> l, r, u, d;
  int ls, rs, us, ds;
  for (int i = 0; i < (int)t.size(); i++) {
    int id = fl[i].second;
    int nid = -1;
    if (i + 1 < (int)t.size()) nid = fl[i + 1].second;
    l[id] = nid;
  }
  ls = fl[0].second;
  for (int i = 0; i < (int)t.size(); i++) {
    int id = fr[i].second;
    int nid = -1;
    if (i + 1 < (int)t.size()) nid = fr[i + 1].second;
    r[id] = nid;
  }
  rs = fr[0].second;
  for (int i = 0; i < (int)t.size(); i++) {
    int id = fu[i].second;
    int nid = -1;
    if (i + 1 < (int)t.size()) nid = fu[i + 1].second;
    u[id] = nid;
  }
  us = fu[0].second;
  for (int i = 0; i < (int)t.size(); i++) {
    int id = fd[i].second;
    int nid = -1;
    if (i + 1 < (int)t.size()) nid = fd[i + 1].second;
    d[id] = nid;
  }
  ds = fd[0].second;
  int ri = a[ls] + 1;
  int le = c[rs] - 1;
  int down = g[us] - 1;
  int up = b[ds] + 1;
  int ans = -1;
  int pt = -1;
  for (int i = 0; i < (int)t.size(); i++) {
    int id = fl[i].second;
    if (ri <= a[id]) {
      ans = 0;
      pt = i;
      break;
    } else
      ri = max(ri, c[id]);
    id = fr[i].second;
    if (le >= c[id]) {
      ans = 1;
      pt = i;
      break;
    } else
      le = min(le, a[id]);
    id = fd[i].second;
    if (up <= b[id]) {
      ans = 2;
      pt = i;
      break;
    } else
      up = max(up, g[id]);
    id = fu[i].second;
    if (down >= g[id]) {
      ans = 3;
      pt = i;
      break;
    } else
      down = min(down, b[id]);
  }
  if (pt == -1) return false;
  set<int> del;
  vector<int> temp;
  if (ans == 0) {
    for (int i = 0; i < pt; i++) {
      temp.push_back(fl[i].second);
      del.insert(fl[i].second);
    }
  }
  if (ans == 1) {
    for (int i = 0; i < pt; i++) {
      temp.push_back(fr[i].second);
      del.insert(fr[i].second);
    }
  }
  if (ans == 2) {
    for (int i = 0; i < pt; i++) {
      temp.push_back(fd[i].second);
      del.insert(fd[i].second);
    }
  }
  if (ans == 3) {
    for (int i = 0; i < pt; i++) {
      temp.push_back(fu[i].second);
      del.insert(fu[i].second);
    }
  }
  return run_small(temp) && run_large(l, r, u, d, ls, rs, us, ds, del);
}
bool run_large(map<int, int> &l, map<int, int> &r, map<int, int> &u,
               map<int, int> &d, int ls, int rs, int us, int ds,
               set<int> &del) {
  while (ls != -1 && del.find(ls) != del.end()) ls = l[ls];
  while (rs != -1 && del.find(rs) != del.end()) rs = r[rs];
  while (us != -1 && del.find(us) != del.end()) us = u[us];
  while (ds != -1 && del.find(ds) != del.end()) ds = d[ds];
  int lt = ls, rt = rs, ut = us, dt = ds;
  if (lt == -1 || rt == -1 || ut == -1 || dt == -1) return false;
  int ri = a[ls] + 1;
  int le = c[rs] - 1;
  int up = b[ds] + 1;
  int down = g[us] - 1;
  int ans = -1;
  int pt = -1;
  int cnt = 0;
  while (!(lt == -1 && rt == -1 && ut == -1 && dt == -1)) {
    cnt++;
    if (lt != -1) {
      while (l[lt] != -1 && del.find(l[lt]) != del.end()) l[lt] = l[l[lt]];
      if (ri <= a[lt]) {
        ans = 0;
        pt = lt;
        break;
      } else
        ri = max(ri, c[lt]);
      lt = l[lt];
    }
    if (rt != -1) {
      while (r[rt] != -1 && del.find(r[rt]) != del.end()) r[rt] = r[r[rt]];
      if (le >= c[rt]) {
        ans = 1;
        pt = rt;
        break;
      } else
        le = min(le, a[rt]);
      rt = r[rt];
    }
    if (dt != -1) {
      while (d[dt] != -1 && del.find(d[dt]) != del.end()) d[dt] = d[d[dt]];
      if (up <= b[dt]) {
        ans = 2;
        pt = dt;
        break;
      } else
        up = max(up, g[dt]);
      dt = d[dt];
    }
    if (ut != -1) {
      while (u[ut] != -1 && del.find(u[ut]) != del.end()) u[ut] = u[u[ut]];
      if (down >= g[ut]) {
        ans = 3;
        pt = ut;
        break;
      } else
        down = min(down, b[ut]);
      ut = u[ut];
    }
  }
  if (cnt == 1) return true;
  if (pt == -1) return false;
  vector<int> temp;
  int st;
  if (ans == 0) {
    st = ls;
    while (st != pt) {
      temp.push_back(st);
      del.insert(st);
      st = l[st];
    }
  }
  if (ans == 1) {
    st = rs;
    while (st != pt) {
      temp.push_back(st);
      del.insert(st);
      st = r[st];
    }
  }
  if (ans == 2) {
    st = ds;
    while (st != pt) {
      temp.push_back(st);
      del.insert(st);
      st = d[st];
    }
  }
  if (ans == 3) {
    st = us;
    while (st != pt) {
      temp.push_back(st);
      del.insert(st);
      st = u[st];
    }
  }
  return run_small(temp) && run_large(l, r, u, d, ls, rs, us, ds, del);
}
int main() {
  cin >> n;
  a.assign(n, 0);
  b = c = g = a;
  for (int i = 0; i < n; i++) scanf("%d%d%d%d", &a[i], &b[i], &c[i], &g[i]);
  vector<int> p(n, 0);
  for (int i = 0; i < n; i++) p[i] = i;
  if (run_small(p))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}