#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
// __gnu_pbds::gp_hash_table<int,int> mp;
#define int long long int
#define pb push_back
vector<vector<int>> adj(500005);
vector<int> visit(500005);
int ans=1;
int cn=0,ce=0;
void dfs(int node)
{
     cn++;
     //cout<<node<<endl;
     visit[node]=1;
     for(auto u:adj[node])
     {
          if(!visit[u])
          {
               ce++;
               dfs(u);
          }
     }
}

signed main(){

   // clock_t t = clock();
//********************************************************************************************

 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 int n,x,y,c;
 cin>>n;

 unordered_set<int> us;
 __gnu_pbds::gp_hash_table<int,int> m3;
 map<int,int> mh;
 __gnu_pbds::gp_hash_table<int,tuple<int,int,int>> m;
  vector<int> msv(2*n);
  c=0;
 for(int i=1;i<=n;i++)
 {
       cin>>x>>y;
       msv[c]=x;
       msv[c+1]=y;
       c+=2;
      m[x]={y,i,1};
      m[y]={x,i,-1};
 }
 sort(msv.begin(),msv.end());
 x=0;
 for(auto i:msv)
 {
      if(get<2>(m[i])==1)
      {
           auto itr=mh.lower_bound(i);
           for(;itr!=mh.end();itr++)
           {
                if(itr->first<get<0>(m[i]))
                {
                     x--;
                     if((-x)>=n)
                     {
                          cout<<"NO";
                          return 0;
                     }
                     us.insert(get<1>(m[itr->first]));
                     us.insert(get<1>(m[i]));
                     //cout<<get<1>(m[itr->first])<<" "<<get<1>(m[i])<<endl;
                     adj[get<1>(m[itr->first])].pb(get<1>(m[i]));
                     adj[get<1>(m[i])].pb(get<1>(m[itr->first]));
                }
                else
                {
                     break;
                }
           }
           mh[get<0>(m[i])]=1;
      }
      else
      {
           mh.erase(i);
      }
     //  cout<<i<<"&&& ";
     //  for(auto k:mh)
     //  cout<<k.first<<" ";
     //  cout<<endl;
}
if(us.size()==0)
{
     if(n==1)
     cout<<"YES";
     else
     cout<<"NO";
     return 0;
}

dfs(*us.begin());
if(cn!=ce+1)
{
     cout<<"NO";
     return 0;
}

for(auto i:us)
{
     if(!visit[i])
     {
          cout<<"NO";
          return 0;
     }
}
cout<<"YES";
//********************************************************************************************
    //t = clock() - t;
    //printf ( "Time 1: %lf\n", ( double ) t / CLOCKS_PER_SEC );
}


#include<bits/stdc++.h>
using namespace std;

#define int long long int
#define pb push_back

signed main(){

   // clock_t t = clock();
//********************************************************************************************

 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 int n,x,y;
 cin>>n;
 unordered_map<int,int> mh;
 map<int,int> ms;
 unordered_map<int,pair<int,int>> m;
 for(int i=1;i<=n;i++)
 {
      cin>>x>>y;
      ms[x]++;
      ms[y]--;
      m[x]={y,i};
      m[y]={x,i};
 }
 vector<pair<int,int>> v;
 for(auto i:ms)
 {
     // cout<<i.first<<"*"<<i.second<<endl;
      if(i.second==1)
      {
           for(auto j:mh)
           {
                if(m[j.first].first<m[i.first].first)
                {
       //              cout<<m[j.first].first<<" "<<m[i.first].first<<endl;
                     v.push_back({m[j.first].second,m[i.first].second});
                }
           }
           mh[i.first]=1;
      }
      else
      {
        //cout<<  m[i.first].first<<endl;
           mh.erase(m[i.first].first);
      }
}

mh.clear();
// cout<<v.size()<<endl;
for(auto i:v){
//      cout<<i.first<<" "<<i.second<<endl;
mh[i.first]=1;
mh[i.second]=1;
}

x=mh.size()-v.size();
if(n==1)
x=1;
if(x==1)
cout<<"YES";
else
cout<<"NO";
//********************************************************************************************
    //t = clock() - t;
    //printf ( "Time 1: %lf\n", ( double ) t / CLOCKS_PER_SEC );
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
// __gnu_pbds::gp_hash_table<int,int> mp;
#define int long long int
#define pb push_back

signed main(){

   // clock_t t = clock();
//********************************************************************************************

 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 int n,x,y,c;
 cin>>n;
 __gnu_pbds::gp_hash_table<int,int> mh,m3;
 __gnu_pbds::gp_hash_table<int,tuple<int,int,int>> m;
  vector<int> msv(2*n);
  c=0;
 for(int i=1;i<=n;i++)
 {
       cin>>x>>y;
       msv[c]=x;
       msv[c+1]=y;
       c+=2;
      m[x]={y,i,1};
      m[y]={x,i,-1};
 }
 sort(msv.begin(),msv.end());
 x=0;
 for(auto i:msv)
 {
      if(get<2>(m[i])==1)
      {
           for(auto j:mh)
           {
                if(get<0>(m[j.first])<get<0>(m[i]))
                {
                     x--;
                    m3[get<1>(m[j.first])]=1;
                    m3[get<1>(m[i])]=1;
                }
           }
           mh[i]=1;
      }
      else
      {
           mh.erase(get<0>(m[i]));
      }
}

x+=m3.size();
if(n==1)
x=1;
if(x==1)
cout<<"YES";
else
cout<<"NO";
//********************************************************************************************
    //t = clock() - t;
    //printf ( "Time 1: %lf\n", ( double ) t / CLOCKS_PER_SEC );
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
// __gnu_pbds::gp_hash_table<int,int> mp;
#define int long long int
#define pb push_back

signed main(){

   // clock_t t = clock();
//********************************************************************************************

 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 int n,x,y,c;
 cin>>n;
 __gnu_pbds::gp_hash_table<int,int> m3;
 map<int,int> mh;
 __gnu_pbds::gp_hash_table<int,tuple<int,int,int>> m;
  vector<int> msv(2*n);
  c=0;
 for(int i=1;i<=n;i++)
 {
       cin>>x>>y;
       msv[c]=x;
       msv[c+1]=y;
       c+=2;
      m[x]={y,i,1};
      m[y]={x,i,-1};
 }
 sort(msv.begin(),msv.end());
 x=0;
 for(auto i:msv)
 {
      if(get<2>(m[i])==1)
      {
           auto itr=mh.lower_bound(i);
           for(;itr!=mh.end();itr++)
           {
                if(itr->first<get<0>(m[i]))
                {
                     x--;
                     if((-x)>=n)
                     {
                          cout<<"NO";
                          return 0;
                     }
                   //  cout<<get<1>(m[itr->first])<<" "<<get<1>(m[i])<<endl;
                    m3[get<1>(m[itr->first])]=1;
                    m3[get<1>(m[i])]=1;
                }
                else
                {
                     break;
                }
           }
           mh[get<0>(m[i])]=1;
      }
      else
      {
           mh.erase(i);
      }
     //  cout<<i<<"&&& ";
     //  for(auto k:mh)
     //  cout<<k.first<<" ";
     //  cout<<endl;
}

x+=m3.size();
if(n==1)
x=1;
if(x==1)
cout<<"YES";
else
cout<<"NO";
//********************************************************************************************
    //t = clock() - t;
    //printf ( "Time 1: %lf\n", ( double ) t / CLOCKS_PER_SEC );
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
// __gnu_pbds::gp_hash_table<int,int> mp;
#define int long long int
#define pb push_back
vector<vector<int>> adj(500005);
vector<int> visit(500005);
int ans=1;
int cn=0,ce=0;
void dfs(int node)
{
     cn++;
     //cout<<node<<endl;
     visit[node]=1;
     for(auto u:adj[node])
     {
          if(!visit[u])
          {
               ce++;
               dfs(u);
          }
     }
}

signed main(){

   // clock_t t = clock();
//********************************************************************************************

 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 int n,x,y,c;
 cin>>n;

 unordered_set<int> us;
 __gnu_pbds::gp_hash_table<int,int> m3;
 map<int,int> mh;
 __gnu_pbds::gp_hash_table<int,tuple<int,int,int>> m;
  vector<int> msv(2*n);
  c=0;
 for(int i=1;i<=n;i++)
 {
       cin>>x>>y;
       msv[c]=x;
       msv[c+1]=y;
       c+=2;
      m[x]={y,i,1};
      m[y]={x,i,-1};
 }
 sort(msv.begin(),msv.end());
 x=0;
 for(auto i:msv)
 {
      if(get<2>(m[i])==1)
      {
           auto itr=mh.lower_bound(i);
           for(;itr!=mh.end();itr++)
           {
                if(itr->first<get<0>(m[i]))
                {
                     x--;
                     if((-x)>=n)
                     {
                          cout<<"NO";
                          return 0;
                     }
                     us.insert(get<1>(m[itr->first]));
                     us.insert(get<1>(m[i]));
                     //cout<<get<1>(m[itr->first])<<" "<<get<1>(m[i])<<endl;
                     adj[get<1>(m[itr->first])].pb(get<1>(m[i]));
                     adj[get<1>(m[i])].pb(get<1>(m[itr->first]));
                }
                else
                {
                     break;
                }
           }
           mh[get<0>(m[i])]=1;
      }
      else
      {
           mh.erase(i);
      }
     //  cout<<i<<"&&& ";
     //  for(auto k:mh)
     //  cout<<k.first<<" ";
     //  cout<<endl;
}
if(n==1)
{
     cout<<"NO";
     return 0;
}
dfs(*us.begin());
if(cn!=ce+1)
{
     cout<<"NO";
     return 0;
}

for(auto i:us)
{
     if(!visit[i])
     {
          cout<<"NO";
          return 0;
     }
}
cout<<"YES";
//********************************************************************************************
    //t = clock() - t;
    //printf ( "Time 1: %lf\n", ( double ) t / CLOCKS_PER_SEC );
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
// __gnu_pbds::gp_hash_table<int,int> mp;
#define int long long int
#define pb push_back
vector<vector<int>> adj(500005);
vector<int> visit(500005);
int ans=1;
int cn=0,ce=0;
void dfs(int node)
{
     cn++;
     //cout<<node<<endl;
     visit[node]=1;
     for(auto u:adj[node])
     {
          if(!visit[u])
          {
               ce++;
               dfs(u);
          }
     }
}

signed main(){

   // clock_t t = clock();
//********************************************************************************************

 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 int n,x,y,c;
 cin>>n;

 unordered_set<int> us;
 __gnu_pbds::gp_hash_table<int,int> m3;
 map<int,int> mh;
 __gnu_pbds::gp_hash_table<int,tuple<int,int,int>> m;
  vector<int> msv(2*n);
  c=0;
 for(int i=1;i<=n;i++)
 {
       cin>>x>>y;
       msv[c]=x;
       msv[c+1]=y;
       c+=2;
      m[x]={y,i,1};
      m[y]={x,i,-1};
 }
 sort(msv.begin(),msv.end());
 x=0;
 for(auto i:msv)
 {
      if(get<2>(m[i])==1)
      {
           auto itr=mh.lower_bound(i);
           for(;itr!=mh.end();itr++)
           {
                if(itr->first<get<0>(m[i]))
                {
                     x--;
                     if((-x)>=n)
                     {
                          cout<<"NO";
                          return 0;
                     }
                     us.insert(get<1>(m[itr->first]));
                     us.insert(get<1>(m[i]));
                     //cout<<get<1>(m[itr->first])<<" "<<get<1>(m[i])<<endl;
                     adj[get<1>(m[itr->first])].pb(get<1>(m[i]));
                     adj[get<1>(m[i])].pb(get<1>(m[itr->first]));
                }
                else
                {
                     break;
                }
           }
           mh[get<0>(m[i])]=1;
      }
      else
      {
           mh.erase(i);
      }
     //  cout<<i<<"&&& ";
     //  for(auto k:mh)
     //  cout<<k.first<<" ";
     //  cout<<endl;
}
if(n==1)
{
     cout<<"YES";
     return 0;
}
dfs(*us.begin());
if(cn!=ce+1)
{
     cout<<"NO";
     return 0;
}

for(auto i:us)
{
     if(!visit[i])
     {
          cout<<"NO";
          return 0;
     }
}
cout<<"YES";
//********************************************************************************************
    //t = clock() - t;
    //printf ( "Time 1: %lf\n", ( double ) t / CLOCKS_PER_SEC );
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
// __gnu_pbds::gp_hash_table<int,int> mp;
#define int long long int
#define pb push_back

signed main(){

   // clock_t t = clock();
//********************************************************************************************

 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 int n,x,y;
 cin>>n;
 __gnu_pbds::gp_hash_table<int,int> mh,ms;
 __gnu_pbds::gp_hash_table<int,pair<int,int>> m;
 for(int i=1;i<=n;i++)
 {
      cin>>x>>y;
      ms[x]++;
      ms[y]--;
      m[x]={y,i};
      m[y]={x,i};
 }
 vector<int> msv;
 for(auto i:ms)
 msv.push_back(i.first);
 sort(msv.begin(),msv.end());
 vector<pair<int,int>> v;
 for(auto i:msv)
 {
     // cout<<i.first<<"*"<<i.second<<endl;
      if(ms[i]==1)
      {
           for(auto j:mh)
           {
                if(m[j.first].first<m[i].first)
                {
       //              cout<<m[j.first].first<<" "<<m[i.first].first<<endl;
                     v.push_back({m[j.first].second,m[i].second});
                }
           }
           mh[i]=1;
      }
      else
      {
        //cout<<  m[i.first].first<<endl;
           mh.erase(m[i].first);
      }
}

mh.clear();
// cout<<v.size()<<endl;
for(auto i:v){
//      cout<<i.first<<" "<<i.second<<endl;
mh[i.first]=1;
mh[i.second]=1;
}

x=mh.size()-v.size();
if(n==1)
x=1;
if(x==1)
cout<<"YES";
else
cout<<"NO";
//********************************************************************************************
    //t = clock() - t;
    //printf ( "Time 1: %lf\n", ( double ) t / CLOCKS_PER_SEC );
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
// __gnu_pbds::gp_hash_table<int,int> mp;
#define int long long int
#define pb push_back

signed main(){

   // clock_t t = clock();
//********************************************************************************************

 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 int n,x,y;
 cin>>n;
 __gnu_pbds::gp_hash_table<int,int> mh;
 map<int,int> ms;
 __gnu_pbds::gp_hash_table<int,pair<int,int>> m;
 for(int i=1;i<=n;i++)
 {
      cin>>x>>y;
      ms[x]++;
      ms[y]--;
      m[x]={y,i};
      m[y]={x,i};
 }
 vector<pair<int,int>> v;
 for(auto i:ms)
 {
     // cout<<i.first<<"*"<<i.second<<endl;
      if(i.second==1)
      {
           for(auto j:mh)
           {
                if(m[j.first].first<m[i.first].first)
                {
       //              cout<<m[j.first].first<<" "<<m[i.first].first<<endl;
                     v.push_back({m[j.first].second,m[i.first].second});
                }
           }
           mh[i.first]=1;
      }
      else
      {
        //cout<<  m[i.first].first<<endl;
           mh.erase(m[i.first].first);
      }
}

mh.clear();
// cout<<v.size()<<endl;
for(auto i:v){
//      cout<<i.first<<" "<<i.second<<endl;
mh[i.first]=1;
mh[i.second]=1;
}

x=mh.size()-v.size();
if(n==1)
x=1;
if(x==1)
cout<<"YES";
else
cout<<"NO";
//********************************************************************************************
    //t = clock() - t;
    //printf ( "Time 1: %lf\n", ( double ) t / CLOCKS_PER_SEC );
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
// __gnu_pbds::gp_hash_table<int,int> mp;
#define int long long int
#define pb push_back
vector<vector<int>> adj(500005);
vector<int> visit(500005);
int ans=1;
int cn=0,ce=0;
void dfs(int node)
{
     cn++;
     //cout<<node<<endl;
     visit[node]=1;
     for(auto u:adj[node])
     {
          if(!visit[u])
          {
               ce++;
               dfs(u);
          }
     }
}

signed main(){

   // clock_t t = clock();
//********************************************************************************************

 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 int n,x,y,c;
 cin>>n;

 unordered_set<int> us;
 __gnu_pbds::gp_hash_table<int,int> m3;
 map<int,int> mh;
 __gnu_pbds::gp_hash_table<int,tuple<int,int,int>> m;
  vector<int> msv(2*n);
  c=0;
 for(int i=1;i<=n;i++)
 {
       cin>>x>>y;
       msv[c]=x;
       msv[c+1]=y;
       c+=2;
      m[x]={y,i,1};
      m[y]={x,i,-1};
 }
 sort(msv.begin(),msv.end());
 x=0;
 for(auto i:msv)
 {
      if(get<2>(m[i])==1)
      {
           auto itr=mh.lower_bound(i);
           for(;itr!=mh.end();itr++)
           {
                if(itr->first<get<0>(m[i]))
                {
                     x--;
                     if((-x)>=n)
                     {
                          cout<<"NO";
                          return 0;
                     }
                     us.insert(get<1>(m[itr->first]));
                     us.insert(get<1>(m[i]));
                     //cout<<get<1>(m[itr->first])<<" "<<get<1>(m[i])<<endl;
                     adj[get<1>(m[itr->first])].pb(get<1>(m[i]));
                     adj[get<1>(m[i])].pb(get<1>(m[itr->first]));
                }
                else
                {
                     break;
                }
           }
           mh[get<0>(m[i])]=1;
      }
      else
      {
           mh.erase(i);
      }
     //  cout<<i<<"&&& ";
     //  for(auto k:mh)
     //  cout<<k.first<<" ";
     //  cout<<endl;
}
 if(n==1){
     cout<<"YES";
     return 0;
 }

if(us.size()!=n)
{
     cout<<"NO";
     return 0;
}

dfs(*us.begin());
if(cn!=ce+1)
{
     cout<<"NO";
     return 0;
}

for(auto i:us)
{
     if(!visit[i])
     {
          cout<<"NO";
          return 0;
     }
}
cout<<"YES";
//********************************************************************************************
    //t = clock() - t;
    //printf ( "Time 1: %lf\n", ( double ) t / CLOCKS_PER_SEC );
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

vector<vector<int>> adj;
vector<int> arr;
vector<int> sizee;
vector<bool> vis;

void initialize(int v){
    arr = vector<int> (v);
    sizee = vector<int> (v);
    for(int i=0; i<v; i++){
        arr[i]=i;
        sizee[i]=1;
    }
}

int root(int i){
    while(arr[i]!=i){
        arr[i] = arr[arr[i]];
        i=arr[i];
    }
    return i;
}

void unionn(int a, int b){
    int root_a = root(a);
    int root_b = root(b);
    if(sizee[root_a]<sizee[root_b]){
        arr[root_a]=arr[root_b];
        sizee[root_b]+=sizee[root_a];
    }
    else{
        arr[root_b]=arr[root_a];
        sizee[root_a]+=sizee[root_b];
    }
}

bool find(int a, int b){
    if(root(a)==root(b)) return true;
    else return false;
}

signed main(){
    tezz

    int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        a.push_back({{x,y},i});
    }
    initialize(n);

    adj = vector<vector<int>> (n);
    sort(a.begin(),a.end());
    for(int i=0; i<n; i++){
        int x=a[i].first.second;
        for(int j=i+1; j<n; j++){
            if(a[j].first.first<x && a[j].first.second>x){
                adj[a[i].second].push_back(a[j].second);
            }
            if(a[j].first.first>x)break;
        }
    }
    int c=0;
    set<int> ss;
    for(int i=0; i<n; i++){
        for(int j=0; j<adj[i].size(); j++){
            if(find(i,adj[i][j])){
                cout << "NO" << endl;
                return 0;
            }
            unionn(i,adj[i][j]);
            ss.insert(i);
            ss.insert(adj[i][j]);
            c++;
        }
    }
    if(c!=(n-1) || ss.size()!=n){
        cout << "NO" << endl;
        return 0;
    }
    set<int> s;
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        if(vis[i]){
            s.insert(root(i));
        }
    }
    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

vector<vector<int>> adj;
vector<int> arr;
vector<int> sizee;
vector<bool> vis;

void initialize(int v){
    arr = vector<int> (v);
    sizee = vector<int> (v);
    for(int i=0; i<v; i++){
        arr[i]=i;
        sizee[i]=1;
    }
}

int root(int i){
    while(arr[i]!=i){
        arr[i] = arr[arr[i]];
        i=arr[i];
    }
    return i;
}

void unionn(int a, int b){
    int root_a = root(a);
    int root_b = root(b);
    if(sizee[root_a]<sizee[root_b]){
        arr[root_a]=arr[root_b];
        sizee[root_b]+=sizee[root_a];
    }
    else{
        arr[root_b]=arr[root_a];
        sizee[root_a]+=sizee[root_b];
    }
}

bool find(int a, int b){
    if(root(a)==root(b)) return true;
    else return false;
}

signed main(){
    tezz

    int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        a.push_back({{x,y},i});
    }
    if(n==1){
        cout << "YES" << endl;
        return 0;
    }
    initialize(n);

    adj = vector<vector<int>> (n);
    sort(a.begin(),a.end());
    for(int i=0; i<n; i++){
        int x=a[i].first.second;
        for(int j=i+1; j<n; j++){
            if(a[j].first.first<x && a[j].first.second>x){
                adj[a[i].second].push_back(a[j].second);
            }
            if(a[j].first.first>x)break;
        }
    }
    int c=0;
    set<int> ss;
    for(int i=0; i<n; i++){
        for(int j=0; j<adj[i].size(); j++){
            if(find(i,adj[i][j])){
                cout << "NO" << endl;
                return 0;
            }
            unionn(i,adj[i][j]);
            ss.insert(i);
            ss.insert(adj[i][j]);
            c++;
        }
    }

    //cout << c << " " << ss.size() << endl;
    if(c!=(2*n-1) || ss.size()!=n){
        cout << "NO" << endl;
        return 0;
    }
    set<int> s;
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        if(vis[i]){
            s.insert(root(i));
        }
    }
    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

vector<vector<int>> adj;
vector<int> arr;
vector<int> sizee;
vector<bool> vis;

void initialize(int v){
    arr = vector<int> (v);
    sizee = vector<int> (v);
    for(int i=0; i<v; i++){
        arr[i]=i;
        sizee[i]=1;
    }
}

int root(int i){
    while(arr[i]!=i){
        arr[i] = arr[arr[i]];
        i=arr[i];
    }
    return i;
}

void unionn(int a, int b){
    int root_a = root(a);
    int root_b = root(b);
    if(sizee[root_a]<sizee[root_b]){
        arr[root_a]=arr[root_b];
        sizee[root_b]+=sizee[root_a];
    }
    else{
        arr[root_b]=arr[root_a];
        sizee[root_a]+=sizee[root_b];
    }
}

bool find(int a, int b){
    if(root(a)==root(b)) return true;
    else return false;
}

signed main(){
    tezz

    int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        a.push_back({{x,y},i});
    }
    initialize(n);

    adj = vector<vector<int>> (n);
    sort(a.begin(),a.end());
    for(int i=0; i<n; i++){
        int x=a[i].first.second;
        for(int j=i+1; j<n; j++){
            if(a[j].first.first<x && a[j].first.second>x){
                adj[a[i].second].push_back(a[j].second);
            }
            if(a[j].first.first>x)break;
        }
    }
    int c=0;
    set<int> ss;
    for(int i=0; i<n; i++){
        for(int j=0; j<adj[i].size(); j++){
            if(find(i,adj[i][j])){
                cout << "NO" << endl;
                return 0;
            }
            unionn(i,adj[i][j]);
            ss.insert(i);
            ss.insert(adj[i][j]);
            c++;
        }
    }
    if(c!=(2*n-1) || ss.size()!=2*n){
        cout << "NO" << endl;
        return 0;
    }
    set<int> s;
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        if(vis[i]){
            s.insert(root(i));
        }
    }
    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

vector<vector<int>> adj;
vector<int> arr;
vector<int> sizee;
vector<bool> vis;

void initialize(int v){
    arr = vector<int> (v);
    sizee = vector<int> (v);
    for(int i=0; i<v; i++){
        arr[i]=i;
        sizee[i]=1;
    }
}

int root(int i){
    while(arr[i]!=i){
        arr[i] = arr[arr[i]];
        i=arr[i];
    }
    return i;
}

void unionn(int a, int b){
    int root_a = root(a);
    int root_b = root(b);
    if(sizee[root_a]<sizee[root_b]){
        arr[root_a]=arr[root_b];
        sizee[root_b]+=sizee[root_a];
    }
    else{
        arr[root_b]=arr[root_a];
        sizee[root_a]+=sizee[root_b];
    }
}

bool find(int a, int b){
    if(root(a)==root(b)) return true;
    else return false;
}

signed main(){
    tezz

    int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        a.push_back({{x,y},i});
    }
    initialize(n);

    adj = vector<vector<int>> (n);
    sort(a.begin(),a.end());
    for(int i=0; i<n; i++){
        int x=a[i].first.second;
        for(int j=i+1; j<n; j++){
            if(a[j].first.first<x && a[j].first.second>x){
                adj[a[i].second].push_back(a[j].second);
            }
            if(a[j].first.first>x)break;
        }
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<adj[i].size(); j++){
            if(find(i,adj[i][j])){
                cout << "NO" << endl;
                return 0;
            }
            unionn(i,adj[i][j]);
        }
    }
    set<int> s;
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        if(vis[i]){
            s.insert(root(i));
        }
    }
    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;
int n;
vector<vector<int>> adj;
int countt=0;
vector<bool> vis;
void dfs(int k){
    vis[k]=true;
    countt++;
    for(auto it=adj[k].begin(); it!=adj[k].end(); it++)  {
        if(!vis[*it]){
            dfs(*it);
        }
    }
}

signed main(){
    tezz

    // int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    map<int,pair<int,int>> mp;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        mp[x]={0,i};
        mp[y]={1,x};
    }
    adj = vector<vector<int>> (n);
    vis = vector<bool> (n,false);
    int c=0;
    set<int> s;
    for(auto it: mp){
        if(it.second.first==0){
            s.insert(it.first);
            continue;
        }
        int x=it.second.second;
        auto itt = s.upper_bound(x);
        while(itt!=s.end()){
            int x1=mp[x].second;
            int x2=mp[*itt].second;
            adj[x1].push_back(x2);
            adj[x2].push_back(x1);
            c++;
            if(c==n){
                cout << "NO";
                return 0;
            }
            itt++;
        }
        s.erase(x);
    }
    if(c!=(n-1)){
        cout << "NO";
        return 0;
    }
    dfs(0);
    if(countt==n)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;
int n;
vector<vector<int>> adj;
int countt=0;
vector<bool> vis;
void dfs(int k){
    vis[k]=true;
    countt++;
    for(auto it=adj[k].begin(); it!=adj[k].end(); it++)  {
        if(!vis[*it]){
            dfs(*it);
        }
    }
}

signed main(){
    tezz

    // int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    map<int,pair<int,int>> mp;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        mp[x]={0,i};
        mp[y]={1,x};
    }
    adj = vector<vector<int>> (n);
    vis = vector<bool> (n,false);
    int c=0;
    set<int> s;
    for(auto it: mp){
        if(it.second.first==0){
            s.insert(it.first);
            continue;
        }
        int x=it.second.second;
        auto itt = s.upper_bound(x);
        while(itt!=s.end()){
            int x1=mp[x].second;
            int x2=mp[*itt].second;
            adj[x1].push_back(x2);
            adj[x2].push_back(x1);
            c++;
            itt++;
        }
        s.erase(x);
    }
    if(c!=(n-1)){
        cout << "NO";
        return 0;
    }
    s.clear();
    dfs(0);
    if(countt==n)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

vector<vector<int>> adj;
vector<int> arr;
vector<int> sizee;
vector<bool> vis;

void initialize(int v){
    arr = vector<int> (v);
    sizee = vector<int> (v);
    for(int i=0; i<v; i++){
        arr[i]=i;
        sizee[i]=1;
    }
}

// int root(int i){
//     while(arr[i]!=i){
//         arr[i] = arr[arr[i]];
//         i=arr[i];
//     }
//     return i;
// }

void unionn(int a, int b,int n){
    // int root_a = root(a);
    // int root_b = root(b);
    // if(sizee[root_a]<sizee[root_b]){
    //     arr[root_a]=arr[root_b];
    //     sizee[root_b]+=sizee[root_a];
    // }
    // else{
    //     arr[root_b]=arr[root_a];
    //     sizee[root_a]+=sizee[root_b];
    // }
    for(int i=0; i<n; i++){
        if(arr[i]==arr[a])arr[i]=arr[b];
    }
}

bool find(int a, int b){
    if(arr[a]==arr[b]) return true;
    else return false;
}

signed main(){
    tezz

    int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        a.push_back({{x,y},i});
    }
    initialize(n);

    adj = vector<vector<int>> (n);
    sort(a.begin(),a.end());
    for(int i=0; i<n; i++){
        int x=a[i].first.second;
        for(int j=i+1; j<n; j++){
            if(a[j].first.first<x && a[j].first.second>x){
                adj[a[i].second].push_back(a[j].second);
            }
            if(a[j].first.first>x)break;
        }
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<adj[i].size(); j++){
            if(find(i,adj[i][j])){
                cout << "NO" << endl;
                return 0;
            }
            unionn(i,adj[i][j],n);
        }
    }
    set<int> s;
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        if(vis[i]){
            s.insert(arr[i]);
        }
    }
    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

vector<vector<int>> adj;
vector<int> arr;
vector<int> sizee;
vector<bool> vis;

void initialize(int v){
    arr = vector<int> (v);
    sizee = vector<int> (v);
    for(int i=0; i<v; i++){
        arr[i]=i;
        sizee[i]=1;
    }
}

int root(int i){
    while(arr[i]!=i){
        arr[i] = arr[arr[i]];
        i=arr[i];
    }
    return i;
}

void unionn(int a, int b){
    int root_a = root(a);
    int root_b = root(b);
    if(sizee[root_a]<sizee[root_b]){
        arr[root_a]=arr[root_b];
        sizee[root_b]+=sizee[root_a];
    }
    else{
        arr[root_b]=arr[root_a];
        sizee[root_a]+=sizee[root_b];
    }
}

bool find(int a, int b){
    if(root(a)==root(b)) return true;
    else return false;
}

signed main(){
    tezz

    int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        a.push_back({{x,y},i});
    }
    initialize(n);

    adj = vector<vector<int>> (n);
    sort(a.begin(),a.end());
    for(int i=0; i<n; i++){
        int x=a[i].first.second;
        for(int j=i+1; j<n; j++){
            if(a[j].first.first<x && a[j].first.second>x){
                adj[a[i].second].push_back(a[j].second);
            }
            if(a[j].first.first>x)break;
        }
    }
    int c=0;
    for(int i=0; i<n; i++){
        for(int j=0; j<adj[i].size(); j++){
            if(find(i,adj[i][j])){
                cout << "NO" << endl;
                return 0;
            }
            unionn(i,adj[i][j]);
            c++;
        }
    }
    if(c!=(n-1)){
        cout << "NO" << endl;
        return 0;
    }
    set<int> s;
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        if(vis[i]){
            s.insert(root(i));
        }
    }
    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

vector<vector<int>> adj;
vector<int> arr;
vector<int> sizee;
vector<bool> vis;

void initialize(int v){
    arr = vector<int> (v);
    sizee = vector<int> (v);
    for(int i=0; i<v; i++){
        arr[i]=i;
        sizee[i]=1;
    }
}

int root(int i){
    while(arr[i]!=i){
        arr[i] = arr[arr[i]];
        i=arr[i];
    }
    return i;
}

void unionn(int a, int b){
    int root_a = root(a);
    int root_b = root(b);
    if(sizee[root_a]<sizee[root_b]){
        arr[root_a]=arr[root_b];
        sizee[root_b]+=sizee[root_a];
    }
    else{
        arr[root_b]=arr[root_a];
        sizee[root_a]+=sizee[root_b];
    }
}

bool find(int a, int b){
    if(root(a)==root(b)) return true;
    else return false;
}

signed main(){
    tezz

    int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        a.push_back({{x,y},i});
    }
    if(n==1){
        cout << "YES" << endl;
        return 0;
    }
    initialize(n);

    adj = vector<vector<int>> (n);
    sort(a.begin(),a.end());
    for(int i=0; i<n; i++){
        int x=a[i].first.second;
        for(int j=i+1; j<n; j++){
            if(a[j].first.first<x && a[j].first.second>x){
                adj[a[i].second].push_back(a[j].second);
            }
            if(a[j].first.first>x)break;
        }
    }
    int c=0;
    set<int> ss;
    for(int i=0; i<n; i++){
        for(int j=0; j<adj[i].size(); j++){
            if(find(i,adj[i][j])){
                cout << "NO" << endl;
                return 0;
            }
            unionn(i,adj[i][j]);
            ss.insert(i);
            ss.insert(adj[i][j]);
            c++;
        }
    }

    // cout << c << " " << ss.size() << endl;
    if(c!=(n-1) || ss.size()!=n){
        cout << "NO" << endl;
        return 0;
    }
    set<int> s;
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        if(vis[i]){
            s.insert(root(i));
        }
    }
    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

vector<vector<int>> adj;
int arr[(int)1e5+5];
int sizee[(int)1e5+5];
vector<bool> vis;

void initialize(int v){
    for(int i=0; i<v; i++){
        arr[i]=i;
        sizee[i]=1;
    }
}

int root(int i){
    while(arr[i]!=i){
        arr[i] = arr[arr[i]];
        i=arr[i];
    }
    return i;
}

void unionn(int a, int b){
    int root_a = root(a);
    int root_b = root(b);
    if(sizee[root_a]<sizee[root_b]){
        arr[root_a]=arr[root_b];
        sizee[root_b]+=sizee[root_a];
    }
    else{
        arr[root_b]=arr[root_a];
        sizee[root_a]+=sizee[root_b];
    }
}

bool find(int a, int b){
    if(root(a)==root(b)) return true;
    else return false;
}

signed main(){
    tezz

    int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        a.push_back({{x,y},i});
    }
    initialize(n);

    adj = vector<vector<int>> (n);
    sort(a.begin(),a.end());
    for(int i=0; i<n; i++){
        int x=a[i].first.second;
        for(int j=i+1; j<n; j++){
            if(a[j].first.first<x && a[j].first.second>x){
                adj[a[i].second].push_back(a[j].second);
            }
        }
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<adj[i].size(); j++){
            if(find(i,adj[i][j])){
                cout << "NO" << endl;
                return 0;
            }
            unionn(i,adj[i][j]);
        }
    }
    set<int> s;
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        if(vis[i]){
            s.insert(root(i));
        }
    }
    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

vector<vector<int>> adj;
vector<int> arr;
vector<int> sizee;
vector<bool> vis;

void initialize(int v){
    arr = vector<int> (v);
    sizee = vector<int> (v);
    for(int i=0; i<v; i++){
        arr[i]=i;
        sizee[i]=1;
    }
}

int root(int i){
    while(arr[i]!=i){
        arr[i] = arr[arr[i]];
        i=arr[i];
    }
    return i;
}

void unionn(int a, int b){
    int root_a = root(a);
    int root_b = root(b);
    if(sizee[root_a]<sizee[root_b]){
        arr[root_a]=arr[root_b];
        sizee[root_b]+=sizee[root_a];
    }
    else{
        arr[root_b]=arr[root_a];
        sizee[root_a]+=sizee[root_b];
    }
}

bool find(int a, int b){
    if(root(a)==root(b)) return true;
    else return false;
}

signed main(){
    tezz

    int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        a.push_back({{x,y},i});
    }
    initialize(n);

    adj = vector<vector<int>> (n);
    sort(a.begin(),a.end());
    for(int i=0; i<n; i++){
        int x=a[i].first.second;
        for(int j=i+1; j<n; j++){
            if(a[j].first.first<x && a[j].first.second>x){
                adj[a[i].second].push_back(a[j].second);
            }
        }
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<adj[i].size(); j++){
            if(find(i,adj[i][j])){
                cout << "NO" << endl;
                return 0;
            }
            unionn(i,adj[i][j]);
        }
    }
    set<int> s;
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        if(vis[i]){
            s.insert(root(i));
        }
    }
    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;
int n;
vector<vector<int>> adj;
vector<int> arr;
vector<int> sizee;
vector<bool> vis;

void initialize(int v){
    arr = vector<int> (v);
    for(int i=0; i<v; i++){
        arr[i]=i;
    }
}

void unionn(int a, int b){
    int x=arr[a];
    int y=arr[b];
    for(int i=0; i<n; i++){
        if(arr[i]==x){
            arr[i]=y;
        }
    }
}

signed main(){
    tezz

    // int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    map<int,pair<int,int>> mp;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        mp[x]={0,i};
        mp[y]={1,x};
    }
    initialize(n);
    int c=0;
    set<int> s;
    for(auto it: mp){
        if(it.second.first==0){
            s.insert(it.first);
            continue;
        }
        int x=it.second.second;
        auto itt = s.upper_bound(x);
        while(itt!=s.end()){
            int x1=mp[x].second;
            int x2=mp[*itt].second;
            unionn(x1,x2);
            c++;
            itt++;
        }
        s.erase(x);
    }
    if(c!=(n-1)){
        cout << "NO";
        return 0;
    }
    s.clear();
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        s.insert(arr[i]);
    }

    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    // adj = vector<vector<int>> (n);
    // sort(a.begin(),a.end());
    // for(int i=0; i<n; i++){
    //     int x=a[i].first.second;
    //     for(int j=i+1; j<n; j++){
    //         if(a[j].first.first<x && a[j].first.second>x){
    //             adj[a[i].second].push_back(a[j].second);
    //         }
    //         if(a[j].first.first>x)break;
    //     }
    // }
    // for(int i=0; i<n; i++){
    //     for(int j=0; j<adj[i].size(); j++){
    //         if(find(i,adj[i][j])){
    //             cout << "NO" << endl;
    //             return 0;
    //         }
    //         unionn(i,adj[i][j]);
    //     }
    // }
    // set<int> s;
    // vis = vector<bool> (n,true);
    // for(int i=0; i<n; i++){
    //     if(vis[i]){
    //         s.insert(root(i));
    //     }
    // }
    // if(s.size()==1)cout << "YES" << endl;
    // else cout << "NO" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define int         long long
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

vector<vector<int>> adj;
vector<int> arr;
vector<int> sizee;
vector<bool> vis;

void initialize(int v){
    arr = vector<int> (v);
    sizee = vector<int> (v);
    for(int i=0; i<v; i++){
        arr[i]=i;
        sizee[i]=1;
    }
}

int root(int i){
    while(arr[i]!=i){
        arr[i] = arr[arr[i]];
        i=arr[i];
    }
    return i;
}

void unionn(int a, int b){
    int root_a = root(a);
    int root_b = root(b);
    if(sizee[root_a]<sizee[root_b]){
        arr[root_a]=arr[root_b];
        sizee[root_b]+=sizee[root_a];
    }
    else{
        arr[root_b]=arr[root_a];
        sizee[root_a]+=sizee[root_b];
    }
}

bool find(int a, int b){
    if(root(a)==root(b)) return true;
    else return false;
}

signed main(){
    tezz

    int n;
    cin >> n;
    vector<pair<pair<int,int>,int>> a;
    map<int,pair<int,int>> mp;
    for(int i=0; i<n; i++){
        int x,y;
        cin >> x >> y;
        mp[x]={0,i};
        mp[y]={1,x};
    }
    initialize(n);
    int c=0;
    set<int> s;
    for(auto it: mp){
        if(it.second.first==0){
            s.insert(it.first);
            continue;
        }
        int x=it.second.second;
        auto itt = s.upper_bound(x);
        while(itt!=s.end()){
            int x1=mp[x].second;
            int x2=mp[*itt].second;
            unionn(x1,x2);
            c++;
            itt++;
        }
        s.erase(x);
    }
    if(c!=(n-1)){
        cout << "NO";
        return 0;
    }
    s.clear();
    vis = vector<bool> (n,true);
    for(int i=0; i<n; i++){
        if(vis[i]){
            s.insert(root(i));
        }
    }
    if(s.size()==1)cout << "YES" << endl;
    else cout << "NO" << endl;

    // adj = vector<vector<int>> (n);
    // sort(a.begin(),a.end());
    // for(int i=0; i<n; i++){
    //     int x=a[i].first.second;
    //     for(int j=i+1; j<n; j++){
    //         if(a[j].first.first<x && a[j].first.second>x){
    //             adj[a[i].second].push_back(a[j].second);
    //         }
    //         if(a[j].first.first>x)break;
    //     }
    // }
    // for(int i=0; i<n; i++){
    //     for(int j=0; j<adj[i].size(); j++){
    //         if(find(i,adj[i][j])){
    //             cout << "NO" << endl;
    //             return 0;
    //         }
    //         unionn(i,adj[i][j]);
    //     }
    // }
    // set<int> s;
    // vis = vector<bool> (n,true);
    // for(int i=0; i<n; i++){
    //     if(vis[i]){
    //         s.insert(root(i));
    //     }
    // }
    // if(s.size()==1)cout << "YES" << endl;
    // else cout << "NO" << endl;

    return 0;
}


#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <stdio.h>

#include <bits/stdc++.h>

#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
pair<int,int> a[N];
set<pair<int,int> > s;
vector<int> v[N];
int cnt;
bool used[N];
bool ok=1;
void dfs(int x,int p=-1){
	cnt++;
	used[x]=1;
	for(int i=0;i<sz(v[x]);i++){
		int to=v[x][i];
		if(to == p)continue;
		if(!used[to])dfs(to,x);
		else ok=0;
	}
}
int main(){

	cin >> n;

	for(int i=1;i<=n;i++){
		cin >> a[i].f >> a[i].s;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		while(!s.empty() && (s.begin())->f<a[i].f){
			s.erase(s.begin());
		}
		for(auto it:s){
			if(a[i].s>it.f){
				v[it.s].pb(i);
				v[i].pb(it.s);
				cnt++;
				//cout << it.s << ' ' << i << endl;
			}
		}
		s.insert({a[i].s,i});
	}

	if(cnt!=n-1){
		cout << "NO" << endl;
		ioi
	}

	dfs(1);

	if(!ok){
		cout << "NO" << endl;
		ioi
	}
	if(cnt!=n){
		cout << "NO" << endl;
		ioi
	}

	cout << "YES" << endl;

	ioi
}


#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <stdio.h>

#include <bits/stdc++.h>

#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
pair<int,int> a[N];
set<pair<int,int> > s;
vector<int> v[N];
int cnt;
bool used[N];
bool ok=1;
void dfs(int x,int p=-1){
	cnt++;
	used[x]=1;
	for(int i=0;i<sz(v[x]);i++){
		int to=v[x][i];
		if(to == p)continue;
		if(!used[to])dfs(to,x);
		else ok=0;
	}
}
int main(){

	cin >> n;

	for(int i=1;i<=n;i++){
		cin >> a[i].f >> a[i].s;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		while(!s.empty() && (s.begin())->f<a[i].f){
			s.erase(s.begin());
		}
		for(auto it:s){
			if(a[i].s>it.f){
				v[it.s].pb(i);
				v[i].pb(it.s);
				cnt++;
				//cout << it.s << ' ' << i << endl;
			}
		}
		s.insert({a[i].s,i});
	}

	if(cnt!=n-1){
		cout << "NO" << endl;
		ioi
	}
	cnt=0;

	dfs(1);

	if(!ok){
		cout << "NO" << endl;
		ioi
	}
	if(cnt!=n){
		cout << "NO" << endl;
		ioi
	}

	cout << "YES" << endl;

	ioi
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
pair<int,int> a[N];
set<pair<int,int> > s;
vector<int> v[N];
int cnt;
bool used[N];
bool ok=1;
void dfs(int x,int p=-1){
	cnt++;
	used[x]=1;
	for(int i=0;i<sz(v[x]);i++){
		int to=v[x][i];
		if(!used[to])dfs(to,x);
	}
}
int main(){

	NFS

	cin >> n;

	for(int i=1;i<=n;i++){
		cin >> a[i].f >> a[i].s;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		while(!s.empty() && (s.begin())->f<a[i].f){
			s.erase(s.begin());
		}
		for(auto it:s){
			if(a[i].s>it.f){
				v[it.s].pb(i);
				v[i].pb(it.s);
				cnt++;
				//cout << it.s << ' ' << i << endl;
			}
			else break;
		}
		if(cnt>n-1){
			cout << "NO" << endl;
			ioi
		}
		s.insert({a[i].s,i});
	}

	if(cnt<n-1){
		cout << "NO" << endl;
		ioi
	}
	cnt=0;

	dfs(1);

	if(!ok){
		cout << "NO" << endl;
		ioi
	}
	if(cnt!=n){
		cout << "NO" << endl;
		ioi
	}

	cout << "YES" << endl;

	ioi
}


#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <stdio.h>

#include <bits/stdc++.h>

#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
pair<int,int> a[N];
set<pair<int,int> > s;
vector<int> v[N];
int cnt;
bool used[N];
bool ok=1;
void dfs(int x,int p=-1){
	cnt++;
	used[x]=1;
	for(int i=0;i<sz(v[x]);i++){
		int to=v[x][i];
		if(to == p)continue;
		if(!used[to])dfs(to,x);
		else ok=0;
	}
}
int main(){

	cin >> n;

	for(int i=1;i<=n;i++){
		cin >> a[i].f >> a[i].s;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		while(!s.empty() && (s.begin())->f<a[i].f){
			s.erase(s.begin());
		}
		for(auto it:s){
			if(a[i].s>it.f){
				v[it.s].pb(i);
				v[i].pb(it.s);
				cnt++;
				//cout << it.s << ' ' << i << endl;
			}
		}
		if(cnt>n-1){
			cout << "NO" << endl;
			ioi
		}
		s.insert({a[i].s,i});
	}

	if(cnt!=n-1){
		cout << "NO" << endl;
		ioi
	}
	cnt=0;

	dfs(1);

	if(!ok){
		cout << "NO" << endl;
		ioi
	}
	if(cnt!=n){
		cout << "NO" << endl;
		ioi
	}

	cout << "YES" << endl;

	ioi
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
int pos[2*N],l[N];
set<int> s;
bool used[N];

int main(){

	//NFS

	//cin >> n;

	scanf("%d",&n);

	for(int i=1;i<=n;i++){
		int x,y;
		//cin >> x >> y;
		scanf("%d%d",&x,&y);
		pos[x]=i;
		pos[y]=i;
		l[i]=x;
	}
	int res=0;
	for(int i=1;i<=2*n;i++){
		if(used[pos[i]]){
			s.erase(pos[i]);
			if(res > n-1){
				printf("NO\n");
				ioi
			}
			for(auto it:s){
				if(l[it] > l[pos[i]])res++;
			}
		}
		else{
			s.insert(pos[i]);
			used[pos[i]]=1;
		}
		if(s.empty() && i < 2*n){
			printf("NO\n");
			ioi
		}
	}

	if(res == n-1) printf("YES\n");
	else printf("NO\n");

	ioi
}


#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <stdio.h>

#include <bits/stdc++.h>

#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
pair<int,int> a[N];
set<pair<int,int> > s;
vector<int> v[N];
int cnt;
bool used[N];
bool ok=1;
void dfs(int x,int p=-1){
	cnt++;
	used[x]=1;
	for(int i=0;i<sz(v[x]);i++){
		int to=v[x][i];
		if(!used[to])dfs(to,x);
	}
}
int main(){

	NFS

	cin >> n;

	for(int i=1;i<=n;i++){
		cin >> a[i].f >> a[i].s;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		while(!s.empty() && (s.begin())->f<a[i].f){
			s.erase(s.begin());
		}
		for(auto it:s){
			if(a[i].s>it.f){
				v[it.s].pb(i);
				v[i].pb(it.s);
				cnt++;
				//cout << it.s << ' ' << i << endl;
			}
		}
		if(cnt>n-1){
			cout << "NO" << endl;
			ioi
		}
		s.insert({a[i].s,i});
	}

	if(cnt<n-1){
		cout << "NO" << endl;
		ioi
	}
	cnt=0;

	dfs(1);

	if(!ok){
		cout << "NO" << endl;
		ioi
	}
	if(cnt!=n){
		cout << "NO" << endl;
		ioi
	}

	cout << "YES" << endl;

	ioi
}


#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <stdio.h>

#include <bits/stdc++.h>

#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
pair<int,int> a[N];
set<pair<int,int> > s;
vector<int> v[N];
int cnt;
bool used[N];
bool ok=1;
void dfs(int x,int p=-1){
	cnt++;
	used[x]=1;
	for(int i=0;i<sz(v[x]);i++){
		int to=v[x][i];
		if(to == p)continue;
		if(!used[to])dfs(to,x);
		else ok=0;
	}
}
int main(){

	cin >> n;

	for(int i=1;i<=n;i++){
		cin >> a[i].f >> a[i].s;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		while(!s.empty() && (s.begin())->f<a[i].f){
			s.erase(s.begin());
		}
		for(auto it:s){
			if(a[i].s>it.f){
				v[it.s].pb(i);
				v[i].pb(it.s);
				//cout << it.s << ' ' << i << endl;
			}
		}
		s.insert({a[i].s,i});
	}

	dfs(1);

	if(!ok){
		cout << "NO" << endl;
		ioi
	}
	if(cnt!=n){
		cout << "NO" << endl;
		ioi
	}

	cout << "YES" << endl;

	ioi
}


#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <stdio.h>

#include <bits/stdc++.h>

#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
int pos[2*N],l[N];
set<int> s;
bool used[N];

int main(){

	//NFS

	//cin >> n;

	scanf("%d",&n);

	for(int i=1;i<=n;i++){
		int x,y;
		//cin >> x >> y;
		scanf("%d%d",&x,&y);
		pos[x]=i;
		pos[y]=i;
		l[i]=x;
	}
	int res=0;
	for(int i=1;i<=2*n;i++){
		if(used[pos[i]]){
			s.erase(pos[i]);
			if(res > n-1){
				printf("NO\n");
				ioi
			}
			for(auto it:s){
				if(l[it] > l[pos[i]])res++;
			}
		}
		else{
			s.insert(pos[i]);
			used[pos[i]]=1;
		}
		if(s.empty() && i < 2*n){
			printf("NO\n");
			ioi
		}
	}

	if(res == n-1) printf("YES\n");
	else printf("NO\n");

	ioi
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)1e6 + 7, MOD = (int)1e9 + 7;

int n;
int pos[N],l[N];
set<int> s;
bool used[N];

int main(){

	NFS

	cin >> n;

	for(int i=1;i<=n;i++){
		int x,y;
		cin >> x >> y;
		pos[x]=i;
		pos[y]=i;
		l[i]=x;
	}
	int res=0;
	for(int i=1;i<=2*n;i++){
		if(used[pos[i]]){
			s.erase(pos[i]);
			if(res > n-1){
				cout << "NO" << endl;
				ioi
			}
			for(auto it:s){
				if(l[it] > l[pos[i]])res++;
			}
		}
		else{
			s.insert(pos[i]);
			used[pos[i]]=1;
		}
		if(s.empty() && i < 2*n){
			cout << "NO" << endl;
			ioi
		}
	}

	if(res == n-1) cout << "YES" << endl;
	else cout << "NO" << endl;

	ioi
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
pair<int,int> a[N];
set<pair<int,int> > s;
vector<int> v[N];
int cnt;
bool used[N];
bool ok=1;
void dfs(int x,int p=-1){
	cnt++;
	used[x]=1;
	for(int i=0;i<sz(v[x]);i++){
		int to=v[x][i];
		if(!used[to])dfs(to,x);
	}
}
int main(){

	NFS

	cin >> n;

	for(int i=1;i<=n;i++){
		cin >> a[i].f >> a[i].s;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		while(!s.empty() && (s.begin())->f<a[i].f){
			s.erase(s.begin());
		}
		for(auto it = s.begin(); it != s.end(); it++){
            if((*it).f < a[i].s){
                v[i].pb((*it).s);
                v[(*it).s].pb(i);
                cnt++;
            }else break;
        }
		if(cnt>n-1){
			cout << "NO" << endl;
			ioi
		}
		s.insert({a[i].s,i});
	}

	if(cnt<n-1){
		cout << "NO" << endl;
		ioi
	}
	cnt=0;

	dfs(1);

	if(!ok){
		cout << "NO" << endl;
		ioi
	}
	if(cnt!=n){
		cout << "NO" << endl;
		ioi
	}

	cout << "YES" << endl;

	ioi
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
int pos[N],l[N];
set<int> s;
bool used[N];

int main(){

	NFS

	cin >> n;

	for(int i=1;i<=n;i++){
		int x,y;
		cin >> x >> y;
		pos[x]=i;
		pos[y]=i;
		l[i]=x;
	}
	int res=0;
	for(int i=1;i<=2*n;i++){
		if(used[pos[i]]){
			s.erase(pos[i]);
			if(res > n-1){
				cout << "NO" << endl;
				ioi
			}
			for(auto it:s){
				if(l[it] > l[pos[i]])res++;
			}
		}
		else{
			s.insert(pos[i]);
			used[pos[i]]=1;
		}
		if(s.empty() && i < 2*n){
			cout << "NO" << endl;
			ioi
		}
	}

	if(res == n-1) cout << "YES" << endl;
	else cout << "NO" << endl;

	ioi
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
pair<int,int> a[N];
set<pair<int,int> > s;
vector<int> v[N];
int cnt;
bool used[N];
bool ok=1;
void dfs(int x,int p=-1){
	cnt++;
	used[x]=1;
	for(int i=0;i<sz(v[x]);i++){
		int to=v[x][i];
		if(!used[to])dfs(to,x);
	}
}
int main(){

	NFS

	cin >> n;

	for(int i=1;i<=n;i++){
		cin >> a[i].f >> a[i].s;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		while(!s.empty() && (s.begin())->f<a[i].f){
			s.erase(s.begin());
		}
		for(auto it:s){
			if(a[i].s>it.f){
				v[it.s].pb(i);
				v[i].pb(it.s);
				cnt++;
				//cout << it.s << ' ' << i << endl;
			}
		}
		if(cnt>n-1){
			cout << "NO" << endl;
			ioi
		}
		s.insert({a[i].s,i});
	}

	if(cnt<n-1){
		cout << "NO" << endl;
		ioi
	}
	cnt=0;

	dfs(1);

	if(!ok){
		cout << "NO" << endl;
		ioi
	}
	if(cnt!=n){
		cout << "NO" << endl;
		ioi
	}

	cout << "YES" << endl;

	ioi
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
pair<int,int> a[N];
set<pair<int,int> > s;
vector<int> v[N];
int cnt;
bool used[N];
bool ok=1;
void dfs(int x,int p=-1){
	cnt++;
	used[x]=1;
	for(int i=0;i<sz(v[x]);i++){
		int to=v[x][i];
		if(!used[to])dfs(to,x);
	}
}
int main(){

	NFS

	cin >> n;

	for(int i=1;i<=n;i++){
		cin >> a[i].f >> a[i].s;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		while(!s.empty() && (s.begin())->f<a[i].f){
			s.erase(s.begin());
		}
		for(auto it:s){
			if(a[i].s>it.f){
				v[it.s].pb(i);
				v[i].pb(it.s);
				cnt++;
				//cout << it.s << ' ' << i << endl;
			}
		}
		if(cnt>n-1){
			cout << "NO" << endl;
			ioi
		}
		s.insert({a[i].s,i});
	}

	if(cnt<n-1){
		cout << "NO" << endl;
		ioi
	}
	cnt=0;

	dfs(1);

	if(!ok){
		cout << "NO" << endl;
		ioi
	}
	if(cnt!=n){
		cout << "NO" << endl;
		ioi
	}

	cout << "YES" << endl;

	ioi
}

#define ll long long
#define ld long double
#define ull unsigned ll
#define ioi exit(0);

#define f first
#define s second

#define inf (int)1e9 + 7

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define mp(x,y) make_pair(x,y)

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb push_back
#define ppb pop_back

#define bitcoin __builtin_popcount

#define endl "\n"

#define in(x) insert(x)

#define sz(x) (int)x.size()

#define all(x) x.begin(),x.end()

#define pw2(x) (1ll<<x) //2^x

#define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)

#define sqr(x) ((x) * 1ll * (x))

#define UpdateRandom srand (time(NULL));

using namespace std;

const int N = (int)5e5 + 7, MOD = (int)1e9 + 7;

int n;
pair<int,int> a[N];
set<pair<int,int> > s;
vector<int> v[N];
int cnt;
bool used[N];
bool ok=1;
void dfs(int x,int p=-1){
	cnt++;
	used[x]=1;
	for(int i=0;i<sz(v[x]);i++){
		int to=v[x][i];
		if(!used[to])dfs(to,x);
	}
}
int main(){

	NFS

	cin >> n;

	for(int i=1;i<=n;i++){
		cin >> a[i].f >> a[i].s;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		while(!s.empty() && (s.begin())->f<a[i].f){
			s.erase(s.begin());
		}
		for(auto it:s){
			if(a[i].s>it.f){
				v[it.s].pb(i);
				v[i].pb(it.s);
				cnt++;
				//cout << it.s << ' ' << i << endl;
			}
		}
		if(cnt>n-1){
			cout << "NO" << endl;
			ioi
		}
		s.insert({a[i].s,i});
	}

	if(cnt<n-1){
		cout << "NO" << endl;
		ioi
	}
	cnt=0;

	dfs(1);

	if(!ok){
		cout << "NO" << endl;
		ioi
	}
	if(cnt!=n){
		cout << "NO" << endl;
		ioi
	}

	cout << "YES" << endl;

	ioi
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500005;
const int inf = 1e9 + 5;

int n;
pii niz[maxn];

set<pii> s; // desna granica = fi i indeks = se

vector<int> g[maxn];

bool krug = 0;
int visited[maxn];
void dfs(int v, int p){
    visited[v] = 1;
    for(auto u : g[v]){
        if(!visited[u]){
            dfs(u, v);
            visited[v] += visited[u];
        }
        else krug |= (u != p);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> n;
    ff(i,1,n)cin >> niz[i].fi >> niz[i].se;
    sort(niz + 1, niz + n + 1);
    ff(i,1,n){
        int l = niz[i].fi;
        int r = niz[i].se;
        while(!s.empty() && l > (*s.begin()).fi)s.erase(s.begin());
        for(auto c : s){
            if(r > c.fi){
                g[c.se].pb(i);
                g[i].pb(c.se);
            }
        }
        s.insert({r, i});
    }
    dfs(1, -1);
//    cout << visited[1] << endl;
    cout << (krug || visited[1] != n ? "NO" : "YES") << endl;
    return 0;
}
/**

**/


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500005;
const int inf = 1e9 + 5;

int n;
pii niz[maxn];

set<pii> s; // desna granica = fi i indeks = se

vector<int> g[maxn];

bool krug = 0;
int visited[maxn];
void dfs(int v, int p){
    visited[v] = 1;
    for(auto u : g[v]){
        if(!visited[u]){
            dfs(u, v);
            visited[v] += visited[u];
        }
        else krug |= (u != p);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> n;
    ff(i,1,n)cin >> niz[i].fi >> niz[i].se;
    sort(niz + 1, niz + n + 1);
    ff(i,1,n){
        int l = niz[i].fi;
        int r = niz[i].se;
        while(!s.empty()){
            auto x = *s.begin();
            if(l > x.fi)s.erase(x);
            else break;
        }
        for(auto c : s){
            if(r > c.fi){
                g[c.se].pb(i);
                g[i].pb(c.se);
            }
        }
        s.insert({r, i});
    }
    dfs(1, -1);
//    cout << visited[1] << endl;
    if(krug || visited[1] != n)return cout << "NO", 0;
    cout << "YES" << endl;
    return 0;
}
/**

**/


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500001;
const int inf = 1e9 + 5;

int n;
pii niz[maxn];

set<pii> s; // desna granica = fi i indeks = se

vector<int> g[maxn];

bool krug = 0;
int visited[maxn];
void dfs(int v, int p){
    visited[v] = 1;
    for(auto u : g[v]){
        if(!visited[u]){
            dfs(u, v);
            visited[v] += visited[u];
        }
        else krug |= (u != p);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> n;
    ff(i,1,n)cin >> niz[i].fi >> niz[i].se;
    sort(niz + 1, niz + n + 1);
    ff(i,1,n){
        int l = niz[i].fi;
        int r = niz[i].se;
        while(!s.empty() && l > (*s.begin()).fi)s.erase(s.begin());
        for(auto c : s){
            if(c.fi > r)break;
            g[c.se].pb(i);
            g[i].pb(c.se);
        }
        s.insert({r, i});
    }
    dfs(1, -1);
//    cout << visited[1] << endl;
    cout << (krug || visited[1] != n ? "NO" : "YES") << endl;
    return 0;
}
/**

**/


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500005;
const int inf = 1e9 + 5;

int n;
pii niz[maxn];

set<pii> s; // desna granica = fi i indeks = se

vector<int> g[maxn];

bool krug = 0;
bool visited[maxn];
void dfs(int v, int p){
    visited[v] = 1;
    for(auto u : g[v]){
        if(!visited[u])dfs(u, v);
        else krug |= (u != p);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> n;
    ff(i,1,n)cin >> niz[i].fi >> niz[i].se;
    sort(niz + 1, niz + n + 1);
    ff(i,1,n){
        int l = niz[i].fi;
        int r = niz[i].se;
        while(!s.empty()){
            auto x = *s.begin();
            if(l > x.fi)s.erase(x);
            else break;
        }
        for(auto c : s){
            if(r > c.fi){
                g[c.se].pb(i);
                g[i].pb(c.se);
            }
        }
        s.insert({r, i});
    }
    dfs(1, -1);
    if(krug)return cout << "NO", 0;
    ff(i,1,n){
        if(!visited[i])return cout << "NO", 0;
    }
    cout << "YES" << endl;
    return 0;
}
/**

**/


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500005;
const int inf = 1e9 + 5;

int n;
pii niz[maxn];

set<pii> s; // desna granica = fi i indeks = se

vector<int> g[maxn];

bool krug = 0;
int visited[maxn];
void dfs(int v, int p){
    visited[v] = 1;
    for(auto u : g[v]){
        if(!visited[u]){
            dfs(u, v);
            visited[v] += visited[u];
        }
        else krug |= (u != p);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> n;
    ff(i,1,n)cin >> niz[i].fi >> niz[i].se;
    sort(niz + 1, niz + n + 1);
    ff(i,1,n){
        int l = niz[i].fi;
        int r = niz[i].se;
        while(!s.empty() && l > (*s.begin()).fi)s.erase(s.begin());
        for(auto c : s){
            if(c.fi > r)break;
            if(r > c.fi){
                g[c.se].pb(i);
                g[i].pb(c.se);
            }
        }
        s.insert({r, i});
    }
    dfs(1, -1);
//    cout << visited[1] << endl;
    cout << (krug || visited[1] != n ? "NO" : "YES") << endl;
    return 0;
}
/**

**/


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500005;
int n;
vector<int> g[maxn];
pii niz[maxn];
set<pii> s;
int rez;
void pom(int ind){
	int lijevo = niz[ind].fi, djesno = niz[ind].se;
	while(!s.empty() && (*s.begin()).fi < lijevo)s.erase(s.begin());
	for(auto x:s){
		if(rez >= n)break;
		if(x.fi > djesno)break;
		g[x.se].pb(ind);
		g[ind].pb(x.se);
		rez++;
	}
	s.insert({djesno, ind});
}
int main()
{
	cin >> n;
	for(int i=1;i<=n;i++)cin >> niz[i].fi >> niz[i].se;
	sort(niz+1, niz+n+1);
	for(int i=1;i<=n;i++)pom(i);
	if(rez == n-1)cout << "YES" << endl;
	else cout << "NO" << endl;
	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500005;
int n;
vector<int> g[maxn];
pii niz[maxn];
set<pii> s;
int rez;
int visited[maxn];
bool dfs(int v, int p){
	visited[v] = 1;
	for(auto u:g[v]){
		if(u == p)continue;
		if(visited[u])return 0;
		if(!dfs(u,v))return 0;
		visited[v] += visited[u];
	}
	return 1;
}
void pom(int ind){
	int lijevo = niz[ind].fi, djesno = niz[ind].se;
	while(!s.empty() && (*s.begin()).fi < lijevo)s.erase(s.begin());
	for(auto x:s){
		if(rez >= n)break;
		if(x.fi > djesno)break;
		g[x.se].pb(ind);
		g[ind].pb(x.se);
		rez++;
	}
	s.insert({djesno, ind});
}
int main()
{
	cin >> n;
	for(int i=1;i<=n;i++)cin >> niz[i].fi >> niz[i].se;
	sort(niz+1, niz+n+1);
	for(int i=1;i<=n;i++)pom(i);
	if(!dfs(1,-1))cout << "NO" << endl;
	else{
		if(visited[1] == n)cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 500009;
typedef long long i64;
typedef pair<i64, i64> pi64;
typedef pair<int, int> pi;
const i64 MOD = 119 << 23 | 1;
class {
  public:
    int n;
    pair<pi, int> F[MAXN];
    int G[MAXN * 2], g[MAXN * 2], gg[MAXN * 2];
    void add(int x, int v) {
        for (; x <= 2 * n + 10; x += x & (-x))
            G[x] += v;
    }
    int sum(int x) {
        int res = 0;
        for (; x; x -= x & (-x))
            res += G[x];
        return res;
    }
    bool ok[MAXN];
    void solve() {
        cin >> n;
        for (int i = 0; i < n; ++i) {
            cin >> F[i].first.first >> F[i].first.second;
            F[i].second = i;
            add(F[i].first.first, 1);
            add(F[i].first.second + 1, -1);
            g[F[i].first.first]++;
            gg[F[i].first.second]++;
        }
        if (n == 1) {
            cout << "YES\n";
            return;
        }
        sort(F, F + n, [](pair<pi, int> a, pair<pi, int> b) {
            if (a.first.first == b.first.first)
                return a.first.second > b.first.second;
            return a.first.first < b.first.first;
        });
        int line = 0;
        for (int i = 0; i < n; ++i) {
            int res = sum(F[i].first.second + 1) - g[F[i].first.second + 1];
            if (res > 0)
                ok[F[i].second] = 1;
            line += res;
            add(F[i].first.first, -1);
            add(F[i].first.second + 1, 1);
        }
        if (line != n - 1) {
            cout << "NO\n";
            return;
        }
        sort(F, F + n, [](pair<pi, int> a, pair<pi, int> b) {
            if (a.first.second == b.first.second)
                return a.first.first < b.first.first;
            return a.first.second > b.first.second;
        });
        for (int i = 0; i < n; ++i) {
            add(F[i].first.first, 1);
            add(F[i].first.second + 1, -1);
        }
        for (int i = 0; i < n; ++i) {
            int res = sum(F[i].first.first - 1) - gg[F[i].first.first - 1];
            if (res > 0)
                ok[F[i].second] = 1;
            add(F[i].first.first, -1);
            add(F[i].first.second + 1, 1);
        }
        for (int i = 0; i < n; ++i) {
            if (!ok[i]) {
                cout << "NO\n";
                return;
            }
        }
        cout << "YES\n";
    }
} NSPACE;
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    test = 1;
#endif
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    NSPACE.solve();
}


#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 500009;
typedef long long i64;
typedef pair<i64, i64> pi64;
typedef pair<int, int> pi;
const i64 MOD = 119 << 23 | 1;
class {
  public:
    int n, f[MAXN * 2];
    pi a[MAXN * 2];
    bool un(int x, int y) {
        // cout << x << "---" << y << endl;
        x = fa(x);
        y = fa(y);
        if (x == y)
            return false;
        f[x] = y;
        return true;
    }
    int fa(int x) { return f[x] == x ? x : fa(f[x]); }
    void solve() {
        cin >> n;
        for (int i = 0; i < n; ++i) {
            int l, r;
            cin >> l >> r;
            a[l] = {r, i + 1};
        }
        for (int i = 1; i <= n; ++i)
            f[i] = i;
        set<pi> Se;
        int Line = 0;
        for (int i = 1; i <= 2 * n; ++i) {
            if (a[i].first == 0)
                continue;
            auto L = Se.lower_bound({i, 0}),
                 R = Se.lower_bound({a[i].first, 0});
            for (; L != R; L++) {
                Line++;
                if (Line >= n) {
                    cout << "NO\n";
                    return;
                }
                if (un(a[i].second, (*L).second)) {
                    cout << "NO\n";
                    return;
                }
            }

            Se.insert(a[i]);
        }
        if (Line != n - 1) {
            cout << "NO\n";
            return;
        }
        cout << "YES\n";
    }
} NSPACE;
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    test = 1;
#endif
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    NSPACE.solve();
}


#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 500009;
typedef long long i64;
typedef pair<i64, i64> pi64;
typedef pair<int, int> pi;
const i64 MOD = 119 << 23 | 1;
class {
  public:
    int n, f[MAXN];
    pi a[MAXN];
    void un(int x, int y) {
        // cout << x << "---" << y << endl;
        x = fa(x);
        y = fa(y);
        f[x] = y;
    }
    int fa(int x) { return f[x] == x ? x : fa(f[x]); }
    void solve() {
        cin >> n;
        for (int i = 0; i < n; ++i) {
            int l, r;
            cin >> l >> r;
            a[l] = {r, i + 1};
        }
        for (int i = 1; i <= n; ++i)
            f[i] = i;
        set<pi> Se;
        int Line = 0;
        for (int i = 1; i <= 2 * n; ++i) {
            if (a[i].first == 0)
                continue;
            auto L = Se.lower_bound({i, 0}),
                 R = Se.lower_bound({a[i].first, 0});
            for (; L != R; L++) {
                Line++;
                un(a[i].second, (*L).second);
            }
            if (Line >= n) {
                cout << "NO\n";
                return;
            }
            Se.insert(a[i]);
        }
        if (Line != n - 1) {
            cout << "NO\n";
            return;
        }
        for (int i = 1; i <= n; ++i) {
            if (fa(i) != fa(1)) {
                cout << "NO\n";
                return;
            }
        }
        cout << "YES\n";
    }
} NSPACE;
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    test = 1;
#endif
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    NSPACE.solve();
}


#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 500009;
typedef long long i64;
typedef pair<i64, i64> pi64;
typedef pair<int, int> pi;
const i64 MOD = 119 << 23 | 1;
class {
  public:
    int n, f[MAXN * 2];
    pi a[MAXN * 2];
    bool un(int x, int y) {
        // cout << x << "---" << y << endl;
        x = fa(x);
        y = fa(y);
        if (x == y)
            return false;
        f[x] = y;
        return true;
    }
    int fa(int x) { return f[x] == x ? x : fa(f[x]); }
    void solve() {
        cin >> n;
        for (int i = 0; i < n; ++i) {
            int l, r;
            cin >> l >> r;
            a[l] = {r, i + 1};
        }
        for (int i = 1; i <= n; ++i)
            f[i] = i;
        set<pi> Se;
        int Line = 0;
        for (int i = 1; i <= 2 * n; ++i) {
            if (a[i].first == 0)
                continue;
            auto L = Se.lower_bound({i, 0}),
                 R = Se.lower_bound({a[i].first, 0});
            for (; L != R; L++) {
                Line++;
                if (Line >= n) {
                    cout << "NO\n";
                    return;
                }
                if (!un(a[i].second, (*L).second)) {
                    cout << "NO\n";
                    return;
                }
            }

            Se.insert(a[i]);
        }
        if (Line != n - 1) {
            cout << "NO\n";
            return;
        }
        cout << "YES\n";
    }
} NSPACE;
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    test = 1;
#endif
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    NSPACE.solve();
}


#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 500009;
typedef long long i64;
typedef pair<i64, i64> pi64;
typedef pair<int, int> pi;
const i64 MOD = 119 << 23 | 1;
class {
  public:
    int n, f[MAXN * 2];
    pi a[MAXN * 2];
    bool un(int x, int y) {
        // cout << x << "---" << y << endl;
        x = fa(x);
        y = fa(y);
        if (x == y)
            return false;
        f[x] = y;
        return true;
    }
    int fa(int x) { return f[x] == x ? x : f[x] = fa(f[x]); }
    void solve() {
        cin >> n;
        for (int i = 0; i < n; ++i) {
            int l, r;
            cin >> l >> r;
            a[l] = {r, i + 1};
        }
        for (int i = 1; i <= n; ++i)
            f[i] = i;
        set<pi> Se;
        int Line = 0;
        for (int i = 1; i <= 2 * n; ++i) {
            if (a[i].first == 0)
                continue;
            auto L = Se.lower_bound({i, 0}),
                 R = Se.lower_bound({a[i].first, 0});
            for (; L != R; L++) {
                Line++;
                if (Line >= n) {
                    cout << "NO\n";
                    return;
                }
                if (!un(a[i].second, (*L).second)) {
                    cout << "NO\n";
                    return;
                }
            }

            Se.insert(a[i]);
        }
        if (Line != n - 1) {
            cout << "NO\n";
            return;
        }
        cout << "YES\n";
    }
} NSPACE;
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    test = 1;
#endif
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    NSPACE.solve();
}


#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 500009;
typedef long long i64;
typedef pair<i64, i64> pi64;
typedef pair<int, int> pi;
const i64 MOD = 119 << 23 | 1;
class {
  public:
    int n;
    pair<pi, int> F[MAXN];
    int G[MAXN * 2], g[MAXN * 2], gg[MAXN * 2];
    void add(int x, int v) {
        for (; x <= 2 * n + 10; x += x & (-x))
            G[x] += v;
    }
    int sum(int x) {
        int res = 0;
        for (; x; x -= x & (-x))
            res += G[x];
        return res;
    }
    bool ok[MAXN];
    void solve() {
        cin >> n;
        for (int i = 0; i < n; ++i) {
            cin >> F[i].first.first >> F[i].first.second;
            F[i].second = i;
            add(F[i].first.first, 1);
            add(F[i].first.second + 1, -1);
            g[F[i].first.first]++;
            gg[F[i].first.second]++;
        }
        sort(F, F + n, [](pair<pi, int> a, pair<pi, int> b) {
            if (a.first.first == b.first.first)
                return a.first.second > b.first.second;
            return a.first.first < b.first.first;
        });
        int line = 0;
        for (int i = 0; i < n; ++i) {
            int res = sum(F[i].first.second + 1) - g[F[i].first.second + 1];
            if (res > 0)
                ok[F[i].second] = 1;
            line += res;
            add(F[i].first.first, -1);
            add(F[i].first.second + 1, 1);
        }
        if (line != n - 1) {
            cout << "NO\n";
            return;
        }
        sort(F, F + n, [](pair<pi, int> a, pair<pi, int> b) {
            if (a.first.second == b.first.second)
                return a.first.first < b.first.first;
            return a.first.second > b.first.second;
        });
        for (int i = 0; i < n; ++i) {
            add(F[i].first.first, 1);
            add(F[i].first.second + 1, -1);
        }
        for (int i = 0; i < n; ++i) {
            int res = sum(F[i].first.first - 1) - gg[F[i].first.first - 1];
            if (res > 0)
                ok[F[i].second] = 1;
            add(F[i].first.first, -1);
            add(F[i].first.second + 1, 1);
        }
        for (int i = 0; i < n; ++i) {
            if (!ok[i]) {
                cout << "NO\n";
                return;
            }
        }
        cout << "YES\n";
    }
} NSPACE;
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    test = 1;
#endif
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    NSPACE.solve();
}


#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 500009;
typedef long long i64;
typedef pair<i64, i64> pi64;
typedef pair<int, int> pi;
const i64 MOD = 119 << 23 | 1;
class {
  public:
    int n, f[MAXN * 2];
    pi a[MAXN * 2];
    void un(int x, int y) {
        // cout << x << "---" << y << endl;
        x = fa(x);
        y = fa(y);
        f[x] = y;
    }
    int fa(int x) { return f[x] == x ? x : fa(f[x]); }
    void solve() {
        cin >> n;
        for (int i = 0; i < n; ++i) {
            int l, r;
            cin >> l >> r;
            a[l] = {r, i + 1};
        }
        for (int i = 1; i <= n; ++i)
            f[i] = i;
        set<pi> Se;
        int Line = 0;
        for (int i = 1; i <= 2 * n; ++i) {
            if (a[i].first == 0)
                continue;
            auto L = Se.lower_bound({i, 0}),
                 R = Se.lower_bound({a[i].first, 0});
            for (; L != R; L++) {
                Line++;
                if (Line >= n) {
                    cout << "NO\n";
                    return;
                }
                un(a[i].second, (*L).second);
            }

            Se.insert(a[i]);
        }
        if (Line != n - 1) {
            cout << "NO\n";
            return;
        }
        for (int i = 1; i <= n; ++i) {
            if (fa(i) != fa(1)) {
                cout << "NO\n";
                return;
            }
        }
        cout << "YES\n";
    }
} NSPACE;
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    test = 1;
#endif
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    NSPACE.solve();
}


#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 500009;
typedef long long i64;
typedef pair<i64, i64> pi64;
typedef pair<int, int> pi;
const i64 MOD = 119 << 23 | 1;
class {
  public:
    int n, f[MAXN * 2];
    pi a[MAXN * 2];
    void un(int x, int y) {
        // cout << x << "---" << y << endl;
        x = fa(x);
        y = fa(y);
        f[x] = y;
    }
    int fa(int x) { return f[x] == x ? x : fa(f[x]); }
    void solve() {
        cin >> n;
        for (int i = 0; i < n; ++i) {
            int l, r;
            cin >> l >> r;
            a[l] = {r, i + 1};
        }
        for (int i = 1; i <= n; ++i)
            f[i] = i;
        set<pi> Se;
        int Line = 0;
        for (int i = 1; i <= 2 * n; ++i) {
            if (a[i].first == 0)
                continue;
            auto L = Se.lower_bound({i, 0}),
                 R = Se.lower_bound({a[i].first, 0});
            for (; L != R; L++) {
                Line++;
                un(a[i].second, (*L).second);
            }
            if (Line >= n) {
                cout << "NO\n";
                return;
            }
            Se.insert(a[i]);
        }
        if (Line != n - 1) {
            cout << "NO\n";
            return;
        }
        for (int i = 1; i <= n; ++i) {
            if (fa(i) != fa(1)) {
                cout << "NO\n";
                return;
            }
        }
        cout << "YES\n";
    }
} NSPACE;
int main() {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    test = 1;
#endif
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    NSPACE.solve();
}


/************
* LIBRARIES *
************/

#include <bits/stdc++.h>

using namespace std;

/**********
* ALIASES *
**********/

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef pair<int, ii> iii;

/*********
* MACROS *
*********/

#ifndef _LOCAL_DEBUG
#define _LOCAL_DEBUG 0
#endif // !_LOCAL_DEBUG

//? Easier debugging macro for safe and quick prints
//* The optimizer will erase the if statement if _LOCAL_DEBUG is 0
#define debug(X) if(_LOCAL_DEBUG){cerr << "[" << #X << ':' << __LINE__ << "]: " << X << endl;}

/************
* CORE CODE *
************/

//? Pre-process anything needed
void preProcess(){

}

const int MS = 1e6 + 7;

list<int> adj[MS];

bool dfs(int x, vector<bool> &vis, int p){
  if(vis[x]){
    return false;
  }

  vis[x] = true;

  for(int y : adj[x]){
    if(y != p && !dfs(y, vis, x)){
      return false;
    }
  }

  return true;
}

struct Coord{
  int xs, xe, idx;
  friend bool operator<(const Coord& a, const Coord& b){
    return a.xe < b.xe;
  }
  friend bool operator==(const Coord& a, const Coord& b){
    return a.idx == b.idx;
  }
};

//? Solve a case
void solve(){
  int n;
  cin >> n;

  int x, y;

  vii p(n);
  set<Coord> op;
  // vector<Coord> pq(n*2, {0, 0, 0});

  for(int i = 0; i < n; i++){
    cin >> x >> y;
    p[i] = make_pair(x, y);
  }

  sort(p.begin(), p.end());

  for(int i = 0; i < n; i++){
    ii cur = p[i];
    auto it = op.begin();
    while (!op.empty() && (*it).xe <= cur.first)
    {
      op.erase(*it);
      it = op.begin();
    }

    for(; it != op.end(); it++){
      if((*it).xe >= cur.second){
        break;
      }
      adj[(*it).idx].push_back(i);
      adj[i].push_back((*it).idx);
    }

    // push_heap(pq.begin(), pq.end(), )
    op.insert({cur.first, cur.second, i});
  }

  vector<bool> vis(n, false);

  int sp;

  for(sp = 0; sp < n && adj[sp].size() == 0; ){
    sp++;
  }

  bool poss = dfs(sp, vis, -1);

  if(!poss){
    // debug("HERE");
    cout << "NO\n";
    return;
  }

  for(int i = 0; i < n; i++){
    if(!vis[i]){
      // if(adj[i].size() != 0){
        poss = false;
        break;
      // }
    }
  }

  if(!poss){
    cout << "NO\n";
    return;
  }

  cout << "YES\n";
}

int main(){
  //? STDIN & STDOUT redirect
  #if _LOCAL_DEBUG
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  #endif

  // ? Pre-processing
  // preProcess();

  //? Single case
  solve();

  //? Multi case
  // int t;
  // cin >> t;

  // while(t--){
  //   solve();
  // }

  return 0;
}


    /************
    * LIBRARIES *
    ************/

    #include <bits/stdc++.h>

    using namespace std;

    /**********
    * ALIASES *
    **********/

    typedef long long ll;
    typedef unsigned long long llu;
    typedef pair<int, int> ii;
    typedef vector<int> vi;
    typedef vector<ii> vii;
    typedef pair<int, ii> iii;

    /*********
    * MACROS *
    *********/

    #ifndef _LOCAL_DEBUG
    #define _LOCAL_DEBUG 0
    #endif // !_LOCAL_DEBUG

    //? Easier debugging macro for safe and quick prints
    //* The optimizer will erase the if statement if _LOCAL_DEBUG is 0
    #define debug(X) if(_LOCAL_DEBUG){cerr << "[" << #X << ':' << __LINE__ << "]: " << X << endl;}

    /************
    * CORE CODE *
    ************/

    //? Pre-process anything needed
    void preProcess(){

    }

    const int MS = 1e6 + 7;

    list<int> adj[MS];

    bool dfs(int x, vector<bool> &vis, int p){
      if(vis[x]){
        return false;
      }

      vis[x] = true;

      for(int y : adj[x]){
        if(y != p && !dfs(y, vis, x)){
          return false;
        }
      }

      return true;
    }

    // bool bfs(int x, vector<bool> &vis){
    //   queue<ii> q;
    //   q.push({x, -1});

    //   while(!q.empty()){
    //     ii c = q.front();
    //     q.pop();
    //     vis[c.first] = true;

    //     for(int y : adj[c.first]){
    //       if(y == c.second){
    //         continue;
    //       }

    //       if(vis[y]){
    //         return false;
    //       }

    //       q.push({y, c.first});
    //     }
    //   }

    //   return true;
    // }

    struct Coord{
      int xs, xe, idx;
      friend bool operator<(const Coord& a, const Coord& b){
        return a.xe < b.xe;
      }
      friend bool operator==(const Coord& a, const Coord& b){
        return a.idx == b.idx;
      }
    };

    //? Solve a case
    void solve(){
      int n;
      cin >> n;

      int x, y;

      vii p(n);
      set<Coord> op;
      // vector<Coord> pq(n*2, {0, 0, 0});

      for(int i = 0; i < n; i++){
        cin >> x >> y;
        p[i] = make_pair(x, y);
      }

      sort(p.begin(), p.end());

      for(int i = 0; i < n; i++){
        ii cur = p[i];
        auto it = op.begin();
        while (!op.empty() && (*it).xe <= cur.first)
        {
          op.erase(*it);
          it = op.begin();
        }

        for(; it != op.end(); it++){
          if((*it).xe >= cur.second){
            break;
          }
          adj[(*it).idx].push_back(i);
          adj[i].push_back((*it).idx);
        }

        // push_heap(pq.begin(), pq.end(), )
        op.insert({cur.first, cur.second, i});
      }

      vector<bool> vis(n, false);

      bool poss = dfs(0, vis, -1);

      if(!poss){
        cout << "NO\n";
        return;
      }

      for(int i = 0; i < n; i++){
        if(!vis[i]){
          poss = false;
          break;
        }
      }

      if(!poss){
        cout << "NO\n";
        return;
      }

      cout << "YES\n";
    }

    int main(){
      //? STDIN & STDOUT redirect
      #if _LOCAL_DEBUG
      freopen("in.txt", "r", stdin);
      // freopen("out.txt", "w", stdout);
      #endif

      // ? Pre-processing
      // preProcess();

      //? Single case
      solve();

      //? Multi case
      // int t;
      // cin >> t;

      // while(t--){
      //   solve();
      // }

      return 0;
    }


/************
* LIBRARIES *
************/

#include <bits/stdc++.h>

using namespace std;

/**********
* ALIASES *
**********/

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef pair<int, ii> iii;

/*********
* MACROS *
*********/

#ifndef _LOCAL_DEBUG
#define _LOCAL_DEBUG 0
#endif // !_LOCAL_DEBUG

//? Easier debugging macro for safe and quick prints
//* The optimizer will erase the if statement if _LOCAL_DEBUG is 0
#define debug(X) if(_LOCAL_DEBUG){cerr << "[" << #X << ':' << __LINE__ << "]: " << X << endl;}

/************
* CORE CODE *
************/

//? Pre-process anything needed
void preProcess(){

}

const int MS = 1e6 + 7;

vi adj[MS];

bool dfs(int x, vector<bool> &vis, int p){
  if(vis[x]){
    return false;
  }

  vis[x] = true;

  for(int y : adj[x]){
    if(y != p && !dfs(y, vis, x)){
      return false;
    }
  }

  return true;
}

bool iiicmp(const iii& a, const iii& b){
  return a.second.second < b.second.second;
}

struct Coord{
  int xs, xe, idx;
  friend bool operator<(const Coord& a, const Coord& b){
    return a.xe < b.xe;
  }
  friend bool operator==(const Coord& a, const Coord& b){
    return a.idx == b.idx;
  }
};

//? Solve a case
void solve(){
  int n;
  cin >> n;

  int x, y;

  vi seg(MS, 0);
  vii p(n);
  // list<iii> open;
  set<Coord> op;

  for(int i = 0; i < n; i++){
    cin >> x >> y;
    p[i] = make_pair(x, y);
  }

  sort(p.begin(), p.end());

  // for(int i = 0; i < n; i++){
  //   cerr << p[i].first << " " << p[i].second << endl;
  // }

  for(int i = 0; i < n; i++){
    ii cur = p[i];
      auto it = op.begin();
      while (!op.empty() && (*it).xe < cur.first)
      {
        op.erase(*it);
        it = op.begin();
      }

    for(Coord o: op){
      if(o.xe < cur.second){
        debug(i << " - " << o.idx);

        adj[o.idx].push_back(i);
        adj[i].push_back(o.idx);
      }
    }

    op.insert({cur.first, cur.second, i});
  }

  vector<bool> vis(n, false);

  int sp;

  for(sp = 0; sp < n && adj[sp].size() == 0; ){
    sp++;
  }

  bool poss = dfs(sp, vis, -1);

  if(!poss){
    // debug("HERE");
    cout << "NO\n";
    return;
  }

  for(int i = 0; i < n; i++){
    if(!vis[i]){
      if(adj[i].size() != 0){
        poss = false;
        break;
      }
    }
  }

  if(!poss){
    cout << "NO\n";
    return;
  }

  cout << "YES\n";
}

int main(){
  //? STDIN & STDOUT redirect
  #if _LOCAL_DEBUG
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  #endif

  // ? Pre-processing
  // preProcess();

  //? Single case
  solve();

  //? Multi case
  // int t;
  // cin >> t;

  // while(t--){
  //   solve();
  // }

  return 0;
}