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 INF = (int)1e9;
const int MXN = (int)1e5 + 7;
const int mod = (int)1e9 + 7;
struct Node1 {
  int l, r, b, t, id;
  Node1(){};
  Node1(int _l, int _r, int _b, int _t, int _id)
      : l(_l), r(_r), b(_b), t(_t), id(_id){};
  const bool operator<(const Node1 &p2) const {
    if (l != p2.l)
      return l < p2.l;
    else
      return id < p2.id;
  }
};
struct Node2 {
  int l, r, b, t, id;
  Node2(){};
  Node2(int _l, int _r, int _b, int _t, int _id)
      : l(_l), r(_r), b(_b), t(_t), id(_id){};
  const bool operator<(const Node2 &p2) const {
    if (r != p2.r)
      return r > p2.r;
    else
      return id < p2.id;
  }
};
struct Node3 {
  int l, r, b, t, id;
  Node3(){};
  Node3(int _l, int _r, int _b, int _t, int _id)
      : l(_l), r(_r), b(_b), t(_t), id(_id){};
  const bool operator<(const Node3 &p2) const {
    if (b != p2.b)
      return b < p2.b;
    else
      return id < p2.id;
  }
};
struct Node4 {
  int l, r, b, t, id;
  Node4(){};
  Node4(int _l, int _r, int _b, int _t, int _id)
      : l(_l), r(_r), b(_b), t(_t), id(_id){};
  const bool operator<(const Node4 &p2) const {
    if (t != p2.t)
      return t > p2.t;
    else
      return id < p2.id;
  }
};
Node1 st1[MXN];
Node2 st2[MXN];
Node3 st3[MXN];
Node4 st4[MXN];
bool go(set<Node1> &s1, set<Node2> &s2, set<Node3> &s3, set<Node4> &s4) {
  if (((int)(s1).size()) == 1) return true;
  int n = ((int)(s1).size()) / 2;
  set<Node1> b1;
  set<Node2> b2;
  set<Node3> b3;
  set<Node4> b4;
  int rr = 0, ll = INF;
  int tt = 0, bb = INF;
  for (int i = 0; i < n; i++) {
    Node1 v1 = *s1.begin();
    rr = max(rr, v1.r);
    b1.insert(v1);
    s1.erase(v1);
    if (rr <= s1.begin()->l) {
      for (auto x : b2) s2.insert(x);
      b2.clear();
      for (auto x : b3) s3.insert(x);
      b3.clear();
      for (auto x : b4) s4.insert(x);
      b4.clear();
      for (auto x : b1) {
        s2.erase(st2[x.id]);
        b2.insert(st2[x.id]);
        s3.erase(st3[x.id]);
        b3.insert(st3[x.id]);
        s4.erase(st4[x.id]);
        b4.insert(st4[x.id]);
      }
      if (go(s1, s2, s3, s4) && go(b1, b2, b3, b4))
        return true;
      else
        return false;
    }
    Node2 v2 = *s2.begin();
    ll = min(ll, v2.l);
    b2.insert(v2);
    s2.erase(v2);
    if (ll >= s2.begin()->r) {
      for (auto x : b1) s1.insert(x);
      b1.clear();
      for (auto x : b3) s3.insert(x);
      b3.clear();
      for (auto x : b4) s4.insert(x);
      b4.clear();
      for (auto x : b2) {
        s1.erase(st1[x.id]);
        b1.insert(st1[x.id]);
        s3.erase(st3[x.id]);
        b3.insert(st3[x.id]);
        s4.erase(st4[x.id]);
        b4.insert(st4[x.id]);
      }
      if (go(s1, s2, s3, s4) && go(b1, b2, b3, b4))
        return true;
      else
        return false;
    }
    Node3 v3 = *s3.begin();
    tt = max(tt, v3.t);
    b3.insert(v3);
    s3.erase(v3);
    if (tt <= s3.begin()->b) {
      for (auto x : b1) s1.insert(x);
      b1.clear();
      for (auto x : b2) s2.insert(x);
      b2.clear();
      for (auto x : b4) s4.insert(x);
      b4.clear();
      for (auto x : b3) {
        s1.erase(st1[x.id]);
        b1.insert(st1[x.id]);
        s2.erase(st2[x.id]);
        b2.insert(st2[x.id]);
        s4.erase(st4[x.id]);
        b4.insert(st4[x.id]);
      }
      if (go(s1, s2, s3, s4) && go(b1, b2, b3, b4))
        return true;
      else
        return false;
    }
    Node4 v4 = *s4.begin();
    bb = min(bb, v4.b);
    b4.insert(v4);
    s4.erase(v4);
    if (bb >= s4.begin()->t) {
      for (auto x : b1) s1.insert(x);
      b1.clear();
      for (auto x : b2) s2.insert(x);
      b2.clear();
      for (auto x : b3) s3.insert(x);
      b3.clear();
      for (auto x : b4) {
        s1.erase(st1[x.id]);
        b1.insert(st1[x.id]);
        s2.erase(st2[x.id]);
        b2.insert(st2[x.id]);
        s3.erase(st3[x.id]);
        b3.insert(st3[x.id]);
      }
      if (go(s1, s2, s3, s4) && go(b1, b2, b3, b4))
        return true;
      else
        return false;
    }
  }
  return false;
}
int main() {
  int n;
  scanf("%d", &n);
  set<Node1> s1;
  set<Node2> s2;
  set<Node3> s3;
  set<Node4> s4;
  for (int i = 1; i <= n; i++) {
    int l, r, b, t;
    scanf("%d %d %d %d", &l, &b, &r, &t);
    st1[i] = {l, r, b, t, i};
    st2[i] = {l, r, b, t, i};
    st3[i] = {l, r, b, t, i};
    st4[i] = {l, r, b, t, i};
    s1.insert(st1[i]);
    s2.insert(st2[i]);
    s3.insert(st3[i]);
    s4.insert(st4[i]);
  }
  if (go(s1, s2, s3, s4))
    puts("YES");
  else
    puts("NO");
  return 0;
}