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 q {
  int x1, x2, y1, y2, ind;
};
bool check(list<q> a) {
  int st = a.size();
  if (st < 2) return true;
  unordered_set<int> was;
  auto left = a;
  auto right = a;
  auto up = a;
  auto down = a;
  left.sort([](const q& a, const q& b) { return a.x1 < b.x1; });
  right.sort([](const q& a, const q& b) { return a.x2 > b.x2; });
  up.sort([](const q& a, const q& b) { return a.y1 < b.y1; });
  down.sort([](const q& a, const q& b) { return a.y2 > b.y2; });
  list<q>::iterator it1, it2, it3, it4;
  int cnt1, cnt2, cnt3, cnt4;
  int brd1, brd2, brd3, brd4;
  auto initial = [&]() {
    while (!left.empty() && was.count(left.begin()->ind)) left.pop_front();
    while (!right.empty() && was.count(right.begin()->ind)) right.pop_front();
    while (!up.empty() && was.count(up.begin()->ind)) up.pop_front();
    while (!down.empty() && was.count(down.begin()->ind)) down.pop_front();
    if (left.empty() || right.empty() || up.empty() || down.empty()) return;
    it1 = ++left.begin(), it2 = ++right.begin(), it3 = ++up.begin(),
    it4 = ++down.begin();
    cnt1 = cnt2 = cnt3 = cnt4 = 0;
    brd1 = left.begin()->x2, brd2 = right.begin()->x1, brd3 = up.begin()->y2,
    brd4 = down.begin()->y1;
  };
  initial();
  auto sprt = [&](list<q>& l, list<q>::iterator& it) {
    list<q> res;
    while (l.begin() != it) {
      was.insert(l.begin()->ind);
      res.push_back(*l.begin());
      l.pop_front();
    }
    return res;
  };
  while (it1 != left.end() || it2 != right.end() || it3 != up.end() ||
         it4 != down.end()) {
    if (it1 != left.end()) {
      if (was.count(it1->ind))
        it1 = left.erase(it1);
      else {
        if (brd1 <= it1->x1 && cnt1 * 2 <= left.size()) {
          if (!check(sprt(left, it1))) return false;
          initial();
          continue;
        } else {
          cnt1++;
          brd1 = max(brd1, it1->x2);
          it1++;
        }
      }
    }
    if (it2 != right.end()) {
      if (was.count(it2->ind))
        it2 = right.erase(it2);
      else {
        if (brd2 >= it2->x2 && cnt2 * 2 <= right.size()) {
          if (!check(sprt(right, it2))) return false;
          initial();
          continue;
        } else {
          cnt2++;
          brd2 = min(brd2, it2->x1);
          it2++;
        }
      }
    }
    if (it3 != up.end()) {
      if (was.count(it3->ind))
        it3 = up.erase(it3);
      else {
        if (brd3 <= it3->y1 && cnt3 * 2 <= up.size()) {
          if (!check(sprt(up, it3))) return false;
          initial();
          continue;
        } else {
          cnt3++;
          brd3 = max(brd3, it3->y2);
          it3++;
        }
      }
    }
    if (it4 != down.end()) {
      if (was.count(it4->ind))
        it4 = down.erase(it4);
      else {
        if (brd4 >= it4->y2 && cnt4 * 2 <= down.size()) {
          if (!check(sprt(down, it4))) return false;
          initial();
          continue;
        } else {
          cnt4++;
          brd4 = min(brd4, it4->y1);
          it4++;
        }
      }
    }
  }
  return left.size() < 2 || right.size() < 2 || up.size() < 2 ||
         down.size() < 2;
}
void Solve() {
  int n;
  cin >> n;
  list<q> a;
  for (signed i = 0; i < (n); i++) {
    q z;
    cin >> z.x1 >> z.y1 >> z.x2 >> z.y2;
    z.ind = i;
    a.push_back(z);
  }
  cout << (check(a) ? "YES" : "NO");
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  Solve();
  return 0;
}