#include <bits/stdc++.h>
#define LL __int64

using namespace std;

const int Maxn=1e5+10;
const LL INF=1e16;
int n,k,p,a[Maxn],b[Maxn];
LL dp[22][Maxn];

void Solve(int l,int r,int Left,int Right)
{
    if (l>r) return;
    int m=((l+r)>>1);
    b[a[m]]++;
    LL sum=0;int w=0;
    for (int i=m-1; i>=Left; i--)
    {
        if (i<=Right)
            if (dp[p-1][i]+sum<dp[p][m]) dp[p][m]=dp[p-1][i]+sum,w=i;
        sum+=b[a[i]]++;
    }
    for (int i=m; i>=Left; i--) b[a[i]]--;
    Solve(l,m-1,Left,w);
    Solve(m+1,r,w,Right);
}

int main()
{
//    freopen("f.in","r",stdin);
    for ( ; scanf("%d%d",&n,&k)!=EOF; )
    {
        for (int i=1; i<=n; i++) scanf("%d",&a[i]);
        for (int i=0; i<=k; i++)
        for (int j=0; j<=n; j++) dp[i][j]=INF;
        dp[0][0]=0;
        for (p=1; p<=n; p++) Solve(1,n,0,n-1);
        printf("%I64d\n",dp[k][n]);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
template<class T> inline bool chkmax(T& a,T b){return a<b?a=b,1:0;}
template<class T> inline bool chkmin(T& a,T b){return a>b?a=b,1:0;}
template<typename T> inline T& read(T& x){
    static char c; bool flag=0;
    while(!isdigit(c=getchar())) if(c=='-') flag=1;
    for(x=c-'0';isdigit(c=getchar());(x*=10)+=c-'0');
    if(flag) x=-x;
    return x;
}
typedef long long LL;
const int maxn=100010,maxk=25;
const LL INF=0x3f3f3f3f3f3f3f3fll;
int n,k,c[maxn],a[maxn];
LL f[maxn][maxk];
stack<int> S;
void solve(int l,int r,int L,int R,int t){
    if(l>r) return;
    int mid=(l+r)>>1,p=0;
    LL w=0;
    for(int i=mid-1;i>=L;i--){
        w+=c[a[i+1]]++; S.push(a[i+1]);
        if(chkmin(f[mid][t],f[i][t-1]+w)) p=i;
    }
    while(!S.empty()) --c[S.top()],S.pop();
    solve(l,mid-1,L,p,t); solve(mid+1,r,p,R,t);
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("exec.in","r",stdin);
    freopen("exec.out","w",stdout);
#endif
    read(n); read(k);
    for(int i=1;i<=n;i++) read(a[i]);
    for(int i=0;i<=n;i++) for(int j=0;j<=k;j++) f[i][j]=INF;
    f[0][0]=0;
    for(int i=1;i<=k;i++) solve(1,n,0,n,i);
    printf("%I64d\n",f[n][k]);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
template<class T> inline bool chkmax(T& a,T b){return a<b?a=b,1:0;}
template<class T> inline bool chkmin(T& a,T b){return a>b?a=b,1:0;}
template<typename T> inline T& read(T& x){
    static char c; bool flag=0;
    while(!isdigit(c=getchar())) if(c=='-') flag=1;
    for(x=c-'0';isdigit(c=getchar());(x*=10)+=c-'0');
    if(flag) x=-x;
    return x;
}
typedef long long LL;
const int maxn=100010,maxk=25;
const LL INF=0x3f3f3f3f3f3f3f3fll;
int n,k,a[maxn];
LL f[maxn][maxk];
int c[maxn],lcur=1,rcur=1;
LL w;
void move(int l,int r){
    while(lcur<l) w-=--c[a[lcur++]];
    while(lcur>l) w+=c[a[--lcur]]++;
    while(rcur<r) w+=c[a[++rcur]]++;
    while(rcur>r) w-=--c[a[rcur--]];
}
void solve(int l,int r,int L,int R,int t){
    if(l>r) return;
    int mid=(l+r)>>1,p=0;
    move(L+1,mid);
    for(int i=L;i<=min(mid-1,R);i++){
        if(chkmin(f[mid][t],f[i][t-1]+w)) p=i;
        w-=--c[a[lcur++]];
    }
    solve(l,mid-1,L,p,t); solve(mid+1,r,p,R,t);
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("exec.in","r",stdin);
    freopen("exec.out","w",stdout);
#endif
    read(n); read(k); c[1]=1;
    for(int i=1;i<=n;i++) read(a[i]);
    for(int i=0;i<=n;i++) for(int j=0;j<=k;j++) f[i][j]=INF;
    f[0][0]=0;
    for(int i=1;i<=k;i++) solve(1,n,0,n,i);
    printf("%I64d\n",f[n][k]);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
template<class T> inline bool chkmax(T& a,T b){return a<b?a=b,1:0;}
template<class T> inline bool chkmin(T& a,T b){return a>b?a=b,1:0;}
template<typename T> inline T& read(T& x){
    static char c; bool flag=0;
    while(!isdigit(c=getchar())) if(c=='-') flag=1;
    for(x=c-'0';isdigit(c=getchar());(x*=10)+=c-'0');
    if(flag) x=-x;
    return x;
}
typedef long long LL;
const int maxn=100010,maxk=25;
const LL INF=0x3f3f3f3f3f3f3f3fll;
int n,k,a[maxn];
LL f[maxn][maxk];
int c[maxn],lcur,rcur;
LL w;
void move(int l,int r){
    while(lcur<l) w-=--c[a[lcur++]];
    while(lcur>l) w+=c[a[--lcur]]++;
    while(rcur<r) w+=c[a[++rcur]]++;
    while(rcur>r) w-=--c[a[rcur--]];
}
void solve(int l,int r,int L,int R,int t){
    if(l>r) return;
    int mid=(l+r)>>1,p=0;
    move(L+1,mid);
    for(int i=L;i<=min(mid-1,R);i++){
        if(chkmin(f[mid][t],f[i][t-1]+w)) p=i;
        w-=--c[a[lcur++]];
    }
    solve(l,mid-1,L,p,t); solve(mid+1,r,p,R,t);
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("exec.in","r",stdin);
    freopen("exec.out","w",stdout);
#endif
    read(n); read(k);
    for(int i=1;i<=n;i++) read(a[i]);
    c[a[1]]=lcur=rcur=1;
    for(int i=0;i<=n;i++) for(int j=0;j<=k;j++) f[i][j]=INF;
    f[0][0]=0;
    for(int i=1;i<=k;i++) solve(1,n,0,n,i);
    printf("%I64d\n",f[n][k]);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
template<class T> inline bool chkmax(T& a,T b){return a<b?a=b,1:0;}
template<class T> inline bool chkmin(T& a,T b){return a>b?a=b,1:0;}
template<typename T> inline T& read(T& x){
    static char c; bool flag=0;
    while(!isdigit(c=getchar())) if(c=='-') flag=1;
    for(x=c-'0';isdigit(c=getchar());(x*=10)+=c-'0');
    if(flag) x=-x;
    return x;
}
typedef long long LL;
const int maxn=100010,maxk=25;
const LL INF=0x3f3f3f3f3f3f3f3fll;
int n,k,a[maxn];
LL f[maxn][maxk];
int c[maxn],lcur=1,rcur;
LL w;
void move(int l,int r){
    while(lcur<l) w-=--c[a[lcur++]];
    while(lcur>l) w+=c[a[--lcur]]++;
    while(rcur<r) w+=c[a[++rcur]]++;
    while(rcur>r) w-=--c[a[rcur--]];
}
void solve(int l,int r,int L,int R,int t){
    if(l>r) return;
    int mid=(l+r)>>1,p=0;
    move(L+1,mid);
    for(int i=L;i<=min(mid-1,R);i++){
        if(chkmin(f[mid][t],f[i][t-1]+w)) p=i;
        w-=--c[a[lcur++]];
    }
    solve(l,mid-1,L,p,t); solve(mid+1,r,p,R,t);
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("exec.in","r",stdin);
    freopen("exec.out","w",stdout);
#endif
    read(n); read(k);
    for(int i=1;i<=n;i++) read(a[i]);
    for(int i=0;i<=n;i++) for(int j=0;j<=k;j++) f[i][j]=INF;
    f[0][0]=0;
    for(int i=1;i<=k;i++) solve(1,n,0,n,i);
    printf("%I64d\n",f[n][k]);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#define LL long long
#define CMin(A, B) A < B ? A : B
using namespace std;
const LL MaxN = 110000;
const LL Inf = 2147483647;
LL B[MaxN], K, N;
LL F[MaxN][50];
LL S[MaxN];
LL Ans = 0;
LL Le = 0, Ri = 0;
LL Calc(LL l, LL r) {
    while(Ri < r) {
    	Ri += 1;
        Ans += (LL)(B[S[Ri]]);
        B[S[Ri]] += 1;
    }
    while(Ri > r) {
    	B[S[Ri]] -= 1;
        Ans -= (LL)(B[S[Ri]]);
        Ri -= 1;
    }
    while(Le < l) {
    	B[S[Le]] -= 1;
        Ans -= (LL)(B[S[Le]]);
        Le += 1;
    }
    while(Le > l) {
    	Le -= 1;
        Ans += (LL)(B[S[Le]]);
        B[S[Le]] += 1;
    }
    return Ans;
}
void Slove(LL l, LL r, LL L, LL R, LL Now) {
    if(l > r) return;
    LL Mid = (l + r) / 2;
    LL Min = CMin(Mid, R);
    LL C = 0;
    for(LL i = L; i <= Min; i++) {
        LL M = Calc(i, Mid);
        if(F[i - 1][Now - 1] + M <= F[Mid][Now]) {
            F[Mid][Now] = F[i - 1][Now - 1] + M;
            C = i;
        }
    }
    if(l != r) {
        Slove(l, Mid - 1, L, C, Now);
        Slove(Mid + 1, r, C, R, Now);
    }
}
int main() {
    Le = 1, Ri = 0;
    scanf("%lld%lld", &N, &K);
    F[0][0] = 0;
    for(LL i = 1; i <= N; i++) scanf("%lld", &S[i]);
    memset(F, 0x2f, sizeof(F));
    memset(B, 0, sizeof(B));
    for(LL i = 1; i <= N; i++) F[i][1] = Calc(1, i);
    for(LL i = 2; i <= K; i++) {
    //	memset(B, 0, sizeof(B));
        Slove(1, N, 1, N, i);
    }
    printf("%lld\n", F[N][K]);
}


#include<iostream>
#include<cstring>
using namespace std;
long long n,k,data[100000],flag[1000000]={0},s[1000001],f[1000001][20];
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)
		cin>>data[i];
	 f[1][0]=0;
    memset(flag,0,4*n+4);
    for(int i=1;i<=n;i++)
    {
        f[1][i]=f[1][i-1]+flag[data[i]];
        flag[data[i]]++;
    }
    for(int i=2;i<=k;i++)
    {
        for(int j=i;j<=n;j++)
        {
            int tot=0;
            f[i][j]=0x7FFFFFFF;
            memset(flag,0,4*n+4);
            for(int x=j-1;x>=i-1;x--)
            {
                tot+=flag[data[x+1]];
                f[i][j]=min(f[i-1][x]+tot,f[i][j]);
                flag[data[x+1]]++;
            }
        }
    }
    cout<<f[k][n];
}
//Infinity Loop


#include <cstdio>
#include <algorithm>
#include <cstring>
#define LL long long
#define CMin(A, B) A < B ? A : B
using namespace std;
const LL MaxN = 110;
const LL Inf = 2147483647;
LL B[MaxN], K, N;
LL F[MaxN][MaxN];
LL S[MaxN];
LL Ans = 0;
LL Le = 0, Ri = 0;
LL Calc(LL l, LL r) {
    while(Ri < r) {
    	Ri += 1;
        Ans += (LL)(B[S[Ri]]);
        B[S[Ri]] += 1;
    }
    while(Ri > r) {
    	B[S[Ri]] -= 1;
        Ans -= (LL)(B[S[Ri]]);
        Ri -= 1;
    }
    while(Le < l) {
    	B[S[Le]] -= 1;
        Ans -= (LL)(B[S[Le]]);
        Le += 1;
    }
    while(Le > l) {
    	Le -= 1;
        Ans += (LL)(B[S[Le]]);
        B[S[Le]] += 1;
    }
    return Ans;
}
void Slove(LL l, LL r, LL L, LL R, LL Now) {
    if(l > r) return;
    LL Mid = (l + r) / 2;
    LL Min = CMin(Mid, R);
    LL C = 0;
    for(LL i = L; i <= Min; i++) {
        LL M = Calc(i, Mid);
        if(F[i - 1][Now - 1] + M <= F[Mid][Now]) {
            F[Mid][Now] = F[i - 1][Now - 1] + M;
            C = i;
        }
    }
    if(l != r) {
        Slove(l, Mid - 1, L, C, Now);
        Slove(Mid + 1, r, C, R, Now);
    }
}
int main() {
    Le = 1, Ri = 0;
    scanf("%lld%lld", &N, &K);
    F[0][0] = 0;
    for(LL i = 1; i <= N; i++) scanf("%lld", &S[i]);
    memset(F, 0x2f, sizeof(F));
    memset(B, 0, sizeof(B));
    for(LL i = 1; i <= N; i++) F[i][1] = Calc(1, i);
    for(LL i = 2; i <= K; i++) {
    //	memset(B, 0, sizeof(B));
        Slove(1, N, 1, N, i);
    }
    printf("%lld\n", F[N][K]);
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#define LL long long
#define CMin(A, B) A < B ? A : B
using namespace std;
const LL MaxN = 110;
const LL Inf = 2147483647;
LL B[MaxN], K, N;
LL F[MaxN][MaxN];
LL S[MaxN];
LL Le, Ri;
LL Calc(LL l, LL r) {
	LL Ans = 0;
	while(Ri < r) Ans += B[S[++Ri]]++;
	while(Ri > r) Ans -= --B[S[Ri--]];
	while(Le < l) Ans += B[S[++Le]]++;
	while(Le > l) Ans -= --B[S[Le--]];
	return Ans;
}
void Slove(LL l, LL r, LL L, LL R, LL Now) {
	if(l > r) return;
	LL Mid = (l + r) / 2;
	LL Min = CMin(Mid, R);
	LL K = 0;
	for(LL i = L; i <= Min; i++) {
		LL M = Calc(l, Mid);
		LL A = F[i - 1][Now - 1] + M;
		if(A <= F[Mid][Now]) {
			F[Mid][Now] = A;
			K = i;
		}
	}
	if(l != r) {
		Slove(l, Mid - 1, L, K, Now);
		Slove(Mid + 1, r, K, R, Now);
	}
}
int main() {
	Le = 0, Ri = 1;
	scanf("%lld%lld", &N, &K);
	F[0][0] = 0;
	for(LL i = 1; i <= N; i++) scanf("%lld", &S[i]);
	memset(F, 31, sizeof(F));
	for(LL i = 2; i <= N; i++) F[i][1] = Calc(1, i);
	for(LL i = 2; i <= K; i++) {
	//	memset(B, 0, sizeof(B));
		Slove(1, N, 1, N, i);
	}
	printf("%lld\n", F[N][K]);
}


//by Judge
#include<iostream>
#include<cstring>
#include<cstdio>
#define mid (l+r>>1)
#define ll unsigned long long
using namespace std;
const int M=1e5+111;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){
	int x=0,f=1; char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;
}
ll n,k,a[M],g[M],f[M],now,head,tail,q[M];
void solv(int l,int r,int L,int R){
	if(l>r) return ; int tmp=min(mid,R); ll mv=1e18;
	while(tail<mid) now+=q[a[++tail]]++;
	while(head>tmp+1) now+=q[a[--head]]++;
	while(tail>mid) now-=--q[a[tail--]];
	while(head<tmp+1) now-=--q[a[head++]];
	for(int i=tmp;i>=L;--i){
		int x=a[i]; now+=q[a[--head]]++;
		if(now+g[i-1]<mv) mv=now+g[i-1],tmp=i;
	} f[mid]=mv,solv(l,mid-1,L,tmp),solv(mid+1,r,tmp,R);
}
signed main(){
	n=read(),k=read(),memset(g,0x3f,sizeof(g)),g[0]=0;
	for(int i=1;i<=n;++i) a[i]=read();
	for(int i=1;i<=k;++i){
		head=1,tail=now=0;
		memset(q,0,sizeof(q));
		solv(i,n,i,n),swap(f,g);
	} printf("%I64d\n",g[n]); return 0;
}


//by Judge
#include<iostream>
#include<cstring>
#include<cstdio>
#define mid (l+r>>1)
#define ll unsigned long long
using namespace std;
const int M=1e5+111;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){
	int x=0,f=1; char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;
}
ll n,k,a[M],g[M],f[M],now,head,tail,q[M];
void solv(int l,int r,int L,int R){
	if(l>r) return ; int tmp=min(mid,R); ll mv=1e18;
	while(tail<mid) now+=q[a[++tail]]++;
	while(head>tmp+1) now+=q[a[--head]]++;
	while(tail>mid) now-=--q[a[tail--]];
	while(head<tmp+1) now-=--q[a[head++]];
	for(int i=tmp;i>=L;--i){
		int x=a[i]; now+=q[a[--head]]++;
		if(now+g[i-1]<mv) mv=now+g[i-1],tmp=i;
	} f[mid]=mv,solv(l,mid-1,L,tmp),solv(mid+1,r,tmp,R);
}
signed main(){
	n=read(),k=read(),memset(g,0x3f,sizeof(g)),g[0]=0;
	for(int i=1;i<=n;++i) a[i]=read();
	for(int i=1;i<=k;++i){
		head=1,tail=now=0;
		memset(q,0,sizeof(q));
		solv(i,n,i,n),swap(f,g);
	} printf("%lld\n",g[n]); return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define mem(x,num) memset(x,num,sizeof x)
#define LL unsigned long long
using namespace std;
const int maxn=100006,inf=0x3f3f3f;
int n,k,a[maxn],t[maxn],p,q;
LL ff[maxn],gg[maxn],now;//防止答案过大，用long long存储
LL *f,*g;

void solve(int l1,int r1,int l2,int r2){
    //以l1,r1为标准范围进行二分
    if(l1>r1)return;
    int mid=(l1+r1)/2,temp=min(r2,mid);
    LL mv=1e19;
    while(q<mid)now+=t[a[++q]]++;
    while(p>temp+1)now+=t[a[--p]]++;
    while(q>mid)now-=--t[a[q--]];
    while(p<temp+1)now-=--t[a[p++]];//使p,q不断接近now和temp+1
    dep(i,temp,l2){
        int x=a[i];
        now+=t[a[--p]]++;
        if(now+f[i-1]<mv)mv=now+f[i-1],temp=i;//选取最小的now+ff[i-1]替换mv
    }
    g[mid]=mv;
    solve(l1,mid-1,l2,temp);
    solve(mid+1,r1,temp,r2);
}
int main()
{
    scanf("%d%d",&n,&k);
    rep(i,1,n)scanf("%d",&a[i]);
    mem(ff,inf);
    ff[0]=0;//f是当前状态，g是上一次状态
    f = ff;
    g = gg;
    rep(i,1,k){
        p=1,q=now=0;
        mem(t,0);
        solve(i,n,i,n);
        swap(f,g);
    }
    printf("%I64d\n",f[n]);
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define mem(x,num) memset(x,num,sizeof x)
#define LL unsigned long long
using namespace std;
const int maxn=100006,inf=0x3f3f3f;
int n,k,a[maxn],t[maxn],p,q;
LL f[maxn],g[maxn],now;

void solve(int l1,int r1,int l2,int r2){
	if(l1>r1)return;
	int mid=(l1+r1)/2,temp=std::min(r2,mid);
	LL mv=1<<30;
	while(q<mid)now+=t[a[++q]]++;
	while(p>temp+1)now+=t[a[--p]]++;
	while(q>mid)now-=--t[a[q--]];
	while(p<temp+1)now-=--t[a[p++]];
	dep(i,temp,l2){
		int x=a[i];
		now+=t[a[--p]]++;
		if(now+f[i-1]<mv)mv=now+f[i-1],temp=i;
	}
	g[mid]=mv;
	solve(l1,mid-1,l2,temp);
	solve(mid+1,r1,temp,r2);
}
int main()
{
    scanf("%d%d",&n,&k);
	rep(i,1,n)scanf("%d",&a[i]);
	mem(f,inf);
	f[0]=0;
	rep(i,1,k){
		p=1,q=now=0;
		mem(t,0);
		solve(i,n,i,n);
		memcpy(f,g,sizeof f);
	}
	printf("%I64d\n",f[n]);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define mem(x,num) memset(x,num,sizeof x)
#define LL unsigned long long
using namespace std;
const int maxn=100006,inf=0x3f3f3f;
int n,k,a[maxn],t[maxn],p,q;
LL f[maxn],g[maxn],now;

void solve(int l1,int r1,int l2,int r2){
	if(l1>r1)return;
	int mid=(l1+r1)/2,temp=std::min(r2,mid);
	LL mv=1<<60;
	while(q<mid)now+=t[a[++q]]++;
	while(p>temp+1)now+=t[a[--p]]++;
	while(q>mid)now-=--t[a[q--]];
	while(p<temp+1)now-=--t[a[p++]];
	dep(i,temp,l2){
		int x=a[i];
		now+=t[a[--p]]++;
		if(now+f[i-1]<mv)mv=now+f[i-1],temp=i;
	}
	g[mid]=mv;
	solve(l1,mid-1,l2,temp);
	solve(mid+1,r1,temp,r2);
}
int main()
{
    scanf("%d%d",&n,&k);
	rep(i,1,n)scanf("%d",&a[i]);
	mem(f,inf);
	f[0]=0;
	rep(i,1,k){
		p=1,q=now=0;
		mem(t,0);
		solve(i,n,i,n);
		memcpy(f,g,sizeof f);
	}
	printf("%I64d\n",f[n]);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define mem(x,num) memset(x,num,sizeof x)
#define LL unsigned long long
using namespace std;
const int maxn=100006,inf=0x3f3f3f;
int n,k,a[maxn],t[maxn],p,q;
LL f[maxn],g[maxn],now;

void solve(int l1,int r1,int l2,int r2){
	if(l1>r1)return;
	int mid=(l1+r1)/2,temp=std::min(r2,mid);
	LL mv=inf;
	while(q<mid)now+=t[a[++q]]++;
	while(p>temp+1)now+=t[a[--p]]++;
	while(q>mid)now-=--t[a[q--]];
	while(p<temp+1)now-=--t[a[p++]];
	dep(i,temp,l2){
		int x=a[i];
		now+=t[a[--p]]++;
		if(now+f[i-1]<mv)mv=now+f[i-1],temp=i;
	}
	g[mid]=mv;
	solve(l1,mid-1,l2,temp);
	solve(mid+1,r1,temp,r2);
}
int main()
{
    scanf("%d%d",&n,&k);
	rep(i,1,n)scanf("%d",&a[i]);
	mem(f,inf);
	f[0]=0;
	rep(i,1,k){
		p=1,q=now=0;
		mem(t,0);
		solve(i,n,i,n);
		memcpy(f,g,sizeof f);
	}
	printf("%I64d\n",f[n]);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define mem(x,num) memset(x,num,sizeof x)
#define LL unsigned long long
using namespace std;
const int maxn=100006,inf=0x3f3f3f;
int n,k,a[maxn],t[maxn],p,q;
LL f[maxn],g[maxn],now;

void solve(int l1,int r1,int l2,int r2){
	if(l1>r1)return;
	int mid=(l1+r1)/2,temp=std::min(r2,mid);
	LL mv=1e19;
	while(q<mid)now+=t[a[++q]]++;
	while(p>temp+1)now+=t[a[--p]]++;
	while(q>mid)now-=--t[a[q--]];
	while(p<temp+1)now-=--t[a[p++]];
	dep(i,temp,l2){
		int x=a[i];
		now+=t[a[--p]]++;
		if(now+f[i-1]<mv)mv=now+f[i-1],temp=i;
	}
	g[mid]=mv;
	solve(l1,mid-1,l2,temp);
	solve(mid+1,r1,temp,r2);
}
int main()
{
    scanf("%d%d",&n,&k);
	rep(i,1,n)scanf("%d",&a[i]);
	mem(f,inf);
	f[0]=0;
	rep(i,1,k){
		p=1,q=now=0;
		mem(t,0);
		solve(i,n,i,n);
		memcpy(f,g,sizeof f);
	}
	printf("%I64d\n",f[n]);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define mem(x,num) memset(x,num,sizeof x)
#define LL unsigned long long
using namespace std;
const int maxn=100006,inf=0x3f3f3f;
int n,k,a[maxn],t[maxn],p,q;
LL f[maxn],g[maxn],now;

void solve(int l1,int r1,int l2,int r2){
	if(l1>r1)return;
	int mid=(l1+r1)/2,temp=min(r2,mid);
	LL mv=1e19;
	while(q<mid)now+=t[a[++q]]++;
	while(p>temp+1)now+=t[a[--p]]++;
	while(q>mid)now-=--t[a[q--]];
	while(p<temp+1)now-=--t[a[p++]];
	dep(i,temp,l2){
		int x=a[i];
		now+=t[a[--p]]++;
		if(now+f[i-1]<mv)mv=now+f[i-1],temp=i;
	}
	g[mid]=mv;
	solve(l1,mid-1,l2,temp);
	solve(mid+1,r1,temp,r2);
}
int main()
{
    scanf("%d%d",&n,&k);
	rep(i,1,n)scanf("%d",&a[i]);
	mem(f,inf);
	f[0]=0;
	rep(i,1,k){
		p=1,q=now=0;
		mem(t,0);
		solve(i,n,i,n);
		memcpy(f,g,sizeof f);
	}
	printf("%I64d\n",f[n]);
	return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#define RG register
#define ll long long
const int N = 100050;
using namespace std;
inline int gi(){
	RG char ch=getchar();RG int x=0,q=0;
	while(ch<'0' || ch>'9') ch=='-'?q=1:0,ch=getchar();
	while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
	return q?(-x):x;
}
int p,a[N],ton[N],_l,_r;
ll dp[N][30],s;
inline void divide(int l,int r,int x,int y){
	int mid=(x+y)>>1,t;
	while(_r<mid) s+=ton[a[++_r]]++;
	while(_r>mid) s-=--ton[a[_r--]];
	while(_l<r) s-=--ton[a[_l++]];
	while(_l>r) s+=ton[a[--_l]]++;
	for ( ; _l>=l; s+=ton[a[--_l]]++)
		if (dp[mid][p]>dp[_l-1][p-1]+s)
			dp[mid][p]=dp[_l-1][p-1]+s,t=_l;
	s-=--ton[a[_l++]];
	if (x<mid) divide(l,t,x,mid-1);
	if (y>mid) divide(t,r,mid+1,y);
	return;
}
int main(){
	int n=gi(),k=gi();
	memset(dp,100,sizeof(dp));
	dp[0][0]=0;
	for (int i=1; i<=n; ++i) a[i]=gi();
	for (int i=1; i<=k; ++i){
		memset(ton,0,sizeof(ton)),s=0;
		_l=_r=p=i,--_r,divide(i,n,i,n);
	}
	cout<<dp[n][k];
	return 0;
}


using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define pll pair <ll,ll>
#define pii pair <int,int>
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define all(c) (c).begin(),(c).end()
#define sz(c) (c).size()
#define fn "test"    ///FILE_NAME_HERE
#define rep(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rrep(c,it) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)

template <class T,class U>
void maximize(T &x,U y)
{
    if(x < y)x=y;
}
template <class T,class U>
void minimize(T &x,U y)
{
    if(x > y)x=y;
}
template <class T>
T Abs(T x)
{
    return (x < (T)0 ? -x : x);
}
template <class T,class U,class V>
T addmod(T x,U y,V mod)
{
    return ((x + y)%mod + mod)%mod;
}
template <class T,class U,class V>
T submod(T x,U y,V mod)
{
    return ((x - y)%mod + mod)%mod;
}
template <class T,class U,class V>
T mulmod(T x,U y,V mod)
{
    return (ll)x*y%mod;
}
///---------------------------------------------------------END_TEMPLATE-------------------------------------------------------///

namespace task{

const int N=1e5+5;
const ll inf=1e18;
int cnt[N],a[N];
ll dp[22][N];
ll res=0;
int n,k,cur_l,cur_r;

ll calc(int l,int r)
{
    while(cur_l < l)
    {
        res -= 1LL*cnt[a[cur_l]]*1LL*cnt[a[cur_l]];
        --cnt[a[cur_l]];
        res += 1LL*cnt[a[cur_l]]*1LL*cnt[a[cur_l]];
        ++cur_l;
    }
    while(cur_l > l)
    {
        --cur_l;
        res -= 1LL*cnt[a[cur_l]]*1LL*cnt[a[cur_l]];
        ++cnt[a[cur_l]];
        res += 1LL*cnt[a[cur_l]]*1LL*cnt[a[cur_l]];
    }
    while(cur_r < r)
    {
        ++cur_r;
        res -= 1LL*cnt[a[cur_r]]*1LL*cnt[a[cur_r]];
        ++cnt[a[cur_r]];
        res += 1LL*cnt[a[cur_r]]*1LL*cnt[a[cur_r]];
    }
    while(cur_r > r)
    {
        res -= 1LL*cnt[a[cur_r]]*1LL*cnt[a[cur_r]];
        --cnt[a[cur_r]];
        res += 1LL*cnt[a[cur_r]]*1LL*cnt[a[cur_r]];;
        --cur_r;
    }
    return res;
}

void computeDP(int i,int jleft,int jright,int kleft,int kright)
{
    if(jleft > jright)return ;
    int jmid=(jleft + jright)>>1;
    int bestk=kleft;
    for(int j=kleft;j < jmid;++j)
        if(dp[i][jmid] > dp[i - 1][j] + (calc(j + 1,jmid) - jmid + j)/2)
        {
            dp[i][jmid] = dp[i - 1][j] + (res - jmid + j)/2;
            bestk = j;
        }
    computeDP(i,jleft,jmid - 1,kleft,bestk);
    computeDP(i,jmid + 1,jright,bestk,kright);
}

void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    for(int i=0;i<=n;++i)
        for(int j=1;j<=k;++j)
          dp[j][i] = inf;
    dp[0][0] = 0;
    cur_l=0,cur_r=-1;
    res = 0;
    for(int i=1;i<=n;++i)
        dp[1][i] = (calc(1,i) - i)/2;
    for(int i=2;i<=k;++i)
    {
        cur_l=0,cur_r=-1;
        res = 0;
        fill(cnt + 1,cnt + 1 + n,0);
        computeDP(i,1,n,1,n);
    }
    cout<<dp[k][n];
}}

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(fn".inp","r",stdin);
    freopen(fn".out","w",stdout);
    #endif // ONLINE_JUDGE
    task::solve();
}


using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define pll pair <ll,ll>
#define pii pair <int,int>
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define all(c) (c).begin(),(c).end()
#define sz(c) (c).size()
#define fn "test"    ///FILE_NAME_HERE
#define rep(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rrep(c,it) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)

template <class T,class U>
void maximize(T &x,U y)
{
    if(x < y)x=y;
}
template <class T,class U>
void minimize(T &x,U y)
{
    if(x > y)x=y;
}
template <class T>
T Abs(T x)
{
    return (x < (T)0 ? -x : x);
}
template <class T,class U,class V>
T addmod(T x,U y,V mod)
{
    return ((x + y)%mod + mod)%mod;
}
template <class T,class U,class V>
T submod(T x,U y,V mod)
{
    return ((x - y)%mod + mod)%mod;
}
template <class T,class U,class V>
T mulmod(T x,U y,V mod)
{
    return (ll)x*y%mod;
}
///---------------------------------------------------------END_TEMPLATE-------------------------------------------------------///

namespace task{

const int N=1e5+5;
const ll inf=1e18;
int cnt[N],a[N];
ll dp[22][N];
ll res=0;
int n,k,cur_l,cur_r;

void del(int i)
{
    res += 1 - 2*cnt[a[i]];
    --cnt[a[i]];
}

void add(int i)
{
    res += 1 + 2*cnt[a[i]];
    ++cnt[a[i]];
}

ll calc(int l,int r)
{
    while(cur_l < l)del(cur_l),++cur_l;
    while(cur_l > l)--cur_l,add(cur_l);
    while(cur_r < r)++cur_r,add(cur_r);
    while(cur_r > r)del(cur_r),--cur_r;
    return res;
}

void computeDP(int i,int jleft,int jright,int kleft,int kright)
{
    if(jleft > jright)return ;
    int jmid=(jleft + jright)>>1;
    int bestk=kleft;
    dp[i][jmid] = inf;
    for(int j=min(jmid,kleft);j < kright;++j)
        if(dp[i][jmid] > dp[i - 1][j] + calc(j + 1,jmid))
        {
            dp[i][jmid] = dp[i - 1][j] + res;
            bestk = j;
        }
    computeDP(i,jleft,jmid - 1,kleft,bestk);
    computeDP(i,jmid + 1,jright,bestk,kright);
}

void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    cur_l=0,cur_r=-1;
    res = 0;
    for(int i=1;i<=n;++i)
        dp[1][i] = calc(1,i);
    for(int i=2;i<=k;++i)
    {
        cur_l=0,cur_r=-1;
        res = 0;
        fill(cnt + 1,cnt + 1 + n,0);
        computeDP(i,1,n,1,n);
    }
    cout<<(dp[k][n] - n)/2;
}}

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(fn".inp","r",stdin);
    freopen(fn".out","w",stdout);
    #endif // ONLINE_JUDGE
//    ofstream cout(fn".inp");
//    cout<<100000<<' '<<20<<'\n';
//    for(int i=1;i<=100000;++i)
//        cout<<1 + rand() % 100000<<'\n';
//    cout.close();
    task::solve();
}


using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define pll pair <ll,ll>
#define pii pair <int,int>
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define all(c) (c).begin(),(c).end()
#define sz(c) (c).size()
#define fn "test"    ///FILE_NAME_HERE
#define rep(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rrep(c,it) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)

template <class T,class U>
void maximize(T &x,U y)
{
    if(x < y)x=y;
}
template <class T,class U>
void minimize(T &x,U y)
{
    if(x > y)x=y;
}
template <class T>
T Abs(T x)
{
    return (x < (T)0 ? -x : x);
}
template <class T,class U,class V>
T addmod(T x,U y,V mod)
{
    return ((x + y)%mod + mod)%mod;
}
template <class T,class U,class V>
T submod(T x,U y,V mod)
{
    return ((x - y)%mod + mod)%mod;
}
template <class T,class U,class V>
T mulmod(T x,U y,V mod)
{
    return (ll)x*y%mod;
}
///---------------------------------------------------------END_TEMPLATE-------------------------------------------------------///

namespace task{

const int N=1e5+5;
const ll inf=1e18;
int cnt[N],a[N];
ll dp[22][N];
ll res=0;
int n,k,cur_l,cur_r;

int del(int i)
{
    res += 1 - 2*cnt[a[i]];
    --cnt[a[i]];
}

int add(int i)
{
    res += 1 + 2*cnt[a[i]];
    ++cnt[a[i]];
}

ll calc(int l,int r)
{
    while(cur_l < l)del(cur_l),++cur_l;
    while(cur_l > l)--cur_l,add(cur_l);
    while(cur_r < r)++cur_r,add(cur_r);
    while(cur_r > r)del(cur_r),--cur_r;
    return res;
}

void computeDP(int i,int jleft,int jright,int kleft,int kright)
{
    if(jleft > jright)return ;
    int jmid=(jleft + jright)>>1;
    int bestk=kleft;
    dp[i][jmid] = inf;
    for(int j=kleft;j < min(jmid,kright);++j)
        if(dp[i][jmid] > dp[i - 1][j] + calc(j + 1,jmid))
        {
            dp[i][jmid] = dp[i - 1][j] + res;
            bestk = j;
        }
    computeDP(i,jleft,jmid - 1,kleft,bestk);
    computeDP(i,jmid + 1,jright,bestk,kright);
}

void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    cur_l=0,cur_r=-1;
    res = 0;
    for(int i=1;i<=n;++i)
        dp[1][i] = calc(1,i);
    for(int i=2;i<=k;++i)
    {
        cur_l=0,cur_r=-1;
        res = 0;
        fill(cnt + 1,cnt + 1 + n,0);
        computeDP(i,1,n,1,n);
    }
    cout<<(dp[k][n] - n)/2;
}}

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(fn".inp","r",stdin);
    freopen(fn".out","w",stdout);
    #endif // ONLINE_JUDGE
//    ofstream cout(fn".inp");
//    cout<<100000<<' '<<20<<'\n';
//    for(int i=1;i<=100000;++i)
//        cout<<1 + rand() % 100000<<'\n';
//    cout.close();
    task::solve();
}


using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define pll pair <ll,ll>
#define pii pair <int,int>
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define all(c) (c).begin(),(c).end()
#define sz(c) (c).size()
#define fn "test"    ///FILE_NAME_HERE
#define rep(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rrep(c,it) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)

template <class T,class U>
void maximize(T &x,U y)
{
    if(x < y)x=y;
}
template <class T,class U>
void minimize(T &x,U y)
{
    if(x > y)x=y;
}
template <class T>
T Abs(T x)
{
    return (x < (T)0 ? -x : x);
}
template <class T,class U,class V>
T addmod(T x,U y,V mod)
{
    return ((x + y)%mod + mod)%mod;
}
template <class T,class U,class V>
T submod(T x,U y,V mod)
{
    return ((x - y)%mod + mod)%mod;
}
template <class T,class U,class V>
T mulmod(T x,U y,V mod)
{
    return (ll)x*y%mod;
}
///---------------------------------------------------------END_TEMPLATE-------------------------------------------------------///

namespace task{

const int N=1e5+5;
const ll inf=1e18;
int cnt[N],a[N];
ll dp[22][N];
ll res=0;
int n,k,cur_l,cur_r;

void del(int i)
{
    res += 1 - 2*cnt[a[i]];
    --cnt[a[i]];
}

void add(int i)
{
    res += 1 + 2*cnt[a[i]];
    ++cnt[a[i]];
}

ll calc(int l,int r)
{
    while(cur_l < l)del(cur_l),++cur_l;
    while(cur_l > l)--cur_l,add(cur_l);
    while(cur_r < r)++cur_r,add(cur_r);
    while(cur_r > r)del(cur_r),--cur_r;
    return res;
}

void computeDP(int i,int jleft,int jright,int kleft,int kright)
{
    if(jleft > jright)return ;
    int jmid=(jleft + jright)>>1;
    int bestk=kleft;
    dp[i][jmid] = inf;
    for(int j=kleft;j < min(kright,jmid);++j)
        if(dp[i][jmid] > dp[i - 1][j] + calc(j + 1,jmid))
        {
            dp[i][jmid] = dp[i - 1][j] + res;
            bestk = j;
        }
    computeDP(i,jleft,jmid - 1,kleft,bestk);
    computeDP(i,jmid + 1,jright,bestk,kright);
}

void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    cur_l=0,cur_r=-1;
    res = 0;
    for(int i=1;i<=n;++i)
        dp[1][i] = calc(1,i);
    for(int i=2;i<=k;++i)
    {
        cur_l=0,cur_r=-1;
        res = 0;
        fill(cnt + 1,cnt + 1 + n,0);
        computeDP(i,1,n,1,n);
    }
    cout<<(dp[k][n] - n)/2;
}}

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(fn".inp","r",stdin);
    freopen(fn".out","w",stdout);
    #endif // ONLINE_JUDGE
//    ofstream cout(fn".inp");
//    cout<<100000<<' '<<20<<'\n';
//    for(int i=1;i<=100000;++i)
//        cout<<1 + rand() % 100000<<'\n';
//    cout.close();
    task::solve();
}


using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define pll pair <ll,ll>
#define pii pair <int,int>
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define all(c) (c).begin(),(c).end()
#define sz(c) (c).size()
#define fn "test"    ///FILE_NAME_HERE
#define rep(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rrep(c,it) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)

template <class T,class U>
void maximize(T &x,U y)
{
    if(x < y)x=y;
}
template <class T,class U>
void minimize(T &x,U y)
{
    if(x > y)x=y;
}
template <class T>
T Abs(T x)
{
    return (x < (T)0 ? -x : x);
}
template <class T,class U,class V>
T addmod(T x,U y,V mod)
{
    return ((x + y)%mod + mod)%mod;
}
template <class T,class U,class V>
T submod(T x,U y,V mod)
{
    return ((x - y)%mod + mod)%mod;
}
template <class T,class U,class V>
T mulmod(T x,U y,V mod)
{
    return (ll)x*y%mod;
}
///---------------------------------------------------------END_TEMPLATE-------------------------------------------------------///

namespace task{

const int N=1e5+5;
const ll inf=1e18;
int cnt[N],a[N];
ll dp[22][N];
ll res=0;
int n,k,cur_l,cur_r;

void del(int i)
{
    res += 1 - 2*cnt[a[i]];
    --cnt[a[i]];
}

void add(int i)
{
    res += 1 + 2*cnt[a[i]];
    ++cnt[a[i]];
}

ll calc(int l,int r)
{
    while(cur_l < l)del(cur_l),++cur_l;
    while(cur_l > l)--cur_l,add(cur_l);
    while(cur_r < r)++cur_r,add(cur_r);
    while(cur_r > r)del(cur_r),--cur_r;
    return res;
}

void computeDP(int i,int jleft,int jright,int kleft,int kright)
{
    if(jleft > jright)return ;
    int jmid=(jleft + jright)>>1;
    int bestk=kleft;
    dp[i][jmid] = inf;
    for(int j=kleft;j < kright;++j)
        if(dp[i][jmid] > dp[i - 1][j] + calc(j + 1,jmid))
        {
            dp[i][jmid] = dp[i - 1][j] + res;
            bestk = j;
        }
    computeDP(i,jleft,jmid - 1,kleft,bestk);
    computeDP(i,jmid + 1,jright,bestk,kright);
}

void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    cur_l=0,cur_r=-1;
    res = 0;
    for(int i=1;i<=n;++i)
        dp[1][i] = calc(1,i);
    for(int i=2;i<=k;++i)
    {
        cur_l=0,cur_r=-1;
        res = 0;
        fill(cnt + 1,cnt + 1 + n,0);
        computeDP(i,1,n,1,n);
    }
    cout<<(dp[k][n] - n)/2;
}}

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(fn".inp","r",stdin);
    freopen(fn".out","w",stdout);
    #endif // ONLINE_JUDGE
//    ofstream cout(fn".inp");
//    cout<<100000<<' '<<20<<'\n';
//    for(int i=1;i<=100000;++i)
//        cout<<1 + rand() % 100000<<'\n';
//    cout.close();
    task::solve();
}


using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define pll pair <ll,ll>
#define pii pair <int,int>
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define all(c) (c).begin(),(c).end()
#define sz(c) (c).size()
#define fn "test"    ///FILE_NAME_HERE
#define rep(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rrep(c,it) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)

template <class T,class U>
void maximize(T &x,U y)
{
    if(x < y)x=y;
}
template <class T,class U>
void minimize(T &x,U y)
{
    if(x > y)x=y;
}
template <class T>
T Abs(T x)
{
    return (x < (T)0 ? -x : x);
}
template <class T,class U,class V>
T addmod(T x,U y,V mod)
{
    return ((x + y)%mod + mod)%mod;
}
template <class T,class U,class V>
T submod(T x,U y,V mod)
{
    return ((x - y)%mod + mod)%mod;
}
template <class T,class U,class V>
T mulmod(T x,U y,V mod)
{
    return (ll)x*y%mod;
}
///---------------------------------------------------------END_TEMPLATE-------------------------------------------------------///

namespace task{

const int N=1e5+5;
const ll inf=1e18;
int cnt[N],a[N];
ll dp[22][N];
ll res=0;
int n,k,cur_l,cur_r;

ll calc(int l,int r)
{
    while(cur_l < l)
    {
        res += 1LL - 2LL*cnt[a[cur_l]];
        --cnt[a[cur_l]];
        ++cur_l;
    }
    while(cur_l > l)
    {
        --cur_l;
        res += 1LL + 2LL*cnt[a[cur_l]];
        ++cnt[a[cur_l]];
    }
    while(cur_r < r)
    {
        ++cur_r;
        res += 1LL + 2LL*cnt[a[cur_r]];
        ++cnt[a[cur_r]];
    }
    while(cur_r > r)
    {
        res += 1LL - 2LL*cnt[a[cur_r]];
        --cnt[a[cur_r]];
        --cur_r;
    }
    return res;
}

void computeDP(int i,int jleft,int jright,int kleft,int kright)
{
    if(jleft > jright)return ;
    int jmid=(jleft + jright)>>1;
    int bestk=kleft;
    for(int j=kleft;j < jmid;++j)
        if(dp[i][jmid] > dp[i - 1][j] + (calc(j + 1,jmid) - jmid + j)/2)
        {
            dp[i][jmid] = dp[i - 1][j] + (res - jmid + j)/2;
            bestk = j;
        }
    computeDP(i,jleft,jmid - 1,kleft,bestk);
    computeDP(i,jmid + 1,jright,bestk,kright);
}

void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    for(int i=0;i<=n;++i)
        for(int j=1;j<=k;++j)
          dp[j][i] = inf;
    dp[0][0] = 0;
    cur_l=0,cur_r=-1;
    res = 0;
    for(int i=1;i<=n;++i)
        dp[1][i] = (calc(1,i) - i)/2;
    for(int i=2;i<=k;++i)
    {
        cur_l=0,cur_r=-1;
        res = 0;
        fill(cnt + 1,cnt + 1 + n,0);
        computeDP(i,1,n,1,n);
    }
    cout<<dp[k][n];
}}

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(fn".inp","r",stdin);
    freopen(fn".out","w",stdout);
    #endif // ONLINE_JUDGE
    task::solve();
}


using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define pll pair <ll,ll>
#define pii pair <int,int>
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define all(c) (c).begin(),(c).end()
#define sz(c) (c).size()
#define fn "test"    ///FILE_NAME_HERE
#define rep(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rrep(c,it) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)

template <class T,class U>
void maximize(T &x,U y)
{
    if(x < y)x=y;
}
template <class T,class U>
void minimize(T &x,U y)
{
    if(x > y)x=y;
}
template <class T>
T Abs(T x)
{
    return (x < (T)0 ? -x : x);
}
template <class T,class U,class V>
T addmod(T x,U y,V mod)
{
    return ((x + y)%mod + mod)%mod;
}
template <class T,class U,class V>
T submod(T x,U y,V mod)
{
    return ((x - y)%mod + mod)%mod;
}
template <class T,class U,class V>
T mulmod(T x,U y,V mod)
{
    return (ll)x*y%mod;
}
///---------------------------------------------------------END_TEMPLATE-------------------------------------------------------///

namespace task{

const int N=1e5+5;
const ll inf=1e18;
int cnt[N],a[N];
ll dp[22][N];
ll res=0;
int n,k,cur_l,cur_r;

int del(int i)
{
    res += 1 - 2*cnt[a[i]];
    --cnt[a[i]];
}

int add(int i)
{
    res += 1 + 2*cnt[a[i]];
    ++cnt[a[i]];
}

ll calc(int l,int r)
{
    while(cur_l < l)del(cur_l),++cur_l;
    while(cur_l > l)--cur_l,add(cur_l);
    while(cur_r < r)++cur_r,add(cur_r);
    while(cur_r > r)del(cur_r),--cur_r;
    return res;
}

void computeDP(int i,int jleft,int jright,int kleft,int kright)
{
    if(jleft > jright)return ;
    int jmid=(jleft + jright)>>1;
    int bestk=kleft;
    dp[i][jmid] = inf;
    for(int j=kleft;j < jmid;++j)
        if(dp[i][jmid] > dp[i - 1][j] + calc(j + 1,jmid))
        {
            dp[i][jmid] = dp[i - 1][j] + res;
            bestk = j;
        }
    computeDP(i,jleft,jmid - 1,kleft,bestk);
    computeDP(i,jmid + 1,jright,bestk,kright);
}

void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    cur_l=0,cur_r=-1;
    res = 0;
    for(int i=1;i<=n;++i)
        dp[1][i] = calc(1,i);
    for(int i=2;i<=k;++i)
    {
        cur_l=0,cur_r=-1;
        res = 0;
        fill(cnt + 1,cnt + 1 + n,0);
        computeDP(i,1,n,1,n);
    }
    cout<<(dp[k][n] - n)/2;
}}

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(fn".inp","r",stdin);
    freopen(fn".out","w",stdout);
    #endif // ONLINE_JUDGE
    task::solve();
}


using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define pll pair <ll,ll>
#define pii pair <int,int>
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define all(c) (c).begin(),(c).end()
#define sz(c) (c).size()
#define fn "test"    ///FILE_NAME_HERE
#define rep(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rrep(c,it) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)

template <class T,class U>
void maximize(T &x,U y)
{
    if(x < y)x=y;
}
template <class T,class U>
void minimize(T &x,U y)
{
    if(x > y)x=y;
}
template <class T>
T Abs(T x)
{
    return (x < (T)0 ? -x : x);
}
template <class T,class U,class V>
T addmod(T x,U y,V mod)
{
    return ((x + y)%mod + mod)%mod;
}
template <class T,class U,class V>
T submod(T x,U y,V mod)
{
    return ((x - y)%mod + mod)%mod;
}
template <class T,class U,class V>
T mulmod(T x,U y,V mod)
{
    return (ll)x*y%mod;
}
///---------------------------------------------------------END_TEMPLATE-------------------------------------------------------///

namespace task{

const int N=1e5+5;
const ll inf=1e18;
int cnt[N],a[N];
ll dp[22][N];
ll res=0;
int n,k,cur_l,cur_r;

ll calc(int l,int r)
{
    while(cur_l < l)
    {
        res += 1 - 2*cnt[a[cur_l]];
        --cnt[a[cur_l]];
        ++cur_l;
    }
    while(cur_l > l)
    {
        --cur_l;
        res += 1 + 2*cnt[a[cur_l]];
        ++cnt[a[cur_l]];
    }
    while(cur_r < r)
    {
        ++cur_r;
        res += 1 + 2*cnt[a[cur_r]];
        ++cnt[a[cur_r]];
    }
    while(cur_r > r)
    {
        res += 1 - 2*cnt[a[cur_r]];
        --cnt[a[cur_r]];
        --cur_r;
    }
    return res;
}

void computeDP(int i,int jleft,int jright,int kleft,int kright)
{
    if(jleft > jright)return ;
    int jmid=(jleft + jright)>>1;
    int bestk=kleft;
    dp[i][jmid] = inf;
    for(int j=kleft;j < jmid;++j)
        if(dp[i][jmid] > dp[i - 1][j] + calc(j + 1,jmid))
        {
            dp[i][jmid] = dp[i - 1][j] + res;
            bestk = j;
        }
    computeDP(i,jleft,jmid - 1,kleft,bestk);
    computeDP(i,jmid + 1,jright,bestk,kright);
}

void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    cur_l=0,cur_r=-1;
    res = 0;
    for(int i=1;i<=n;++i)
        dp[1][i] = calc(1,i);
    for(int i=2;i<=k;++i)
    {
        cur_l=0,cur_r=-1;
        res = 0;
        fill(cnt + 1,cnt + 1 + n,0);
        computeDP(i,1,n,1,n);
    }
    cout<<(dp[k][n] - n)/2;
}}

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(fn".inp","r",stdin);
    freopen(fn".out","w",stdout);
    #endif // ONLINE_JUDGE
    task::solve();
}


using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define pll pair <ll,ll>
#define pii pair <int,int>
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define all(c) (c).begin(),(c).end()
#define sz(c) (c).size()
#define fn "test"    ///FILE_NAME_HERE
#define rep(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rrep(c,it) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)

template <class T,class U>
void maximize(T &x,U y)
{
    if(x < y)x=y;
}
template <class T,class U>
void minimize(T &x,U y)
{
    if(x > y)x=y;
}
template <class T>
T Abs(T x)
{
    return (x < (T)0 ? -x : x);
}
template <class T,class U,class V>
T addmod(T x,U y,V mod)
{
    return ((x + y)%mod + mod)%mod;
}
template <class T,class U,class V>
T submod(T x,U y,V mod)
{
    return ((x - y)%mod + mod)%mod;
}
template <class T,class U,class V>
T mulmod(T x,U y,V mod)
{
    return (ll)x*y%mod;
}
///---------------------------------------------------------END_TEMPLATE-------------------------------------------------------///

namespace task{

const int N=1e5+5;
const ll inf=1e18;
int cnt[N],a[N];
ll dp[22][N];
ll res=0;
int n,k,cur_l,cur_r;

int del(int i)
{
    res += 1 - 2*cnt[a[i]];
    --cnt[a[i]];
}

int add(int i)
{
    res += 1 + 2*cnt[a[i]];
    ++cnt[a[i]];
}

ll calc(int l,int r)
{
    while(cur_l < l)del(cur_l),++cur_l;
    while(cur_l > l)--cur_l,add(cur_l);
    while(cur_r < r)++cur_r,add(cur_r);
    while(cur_r > r)del(cur_r),--cur_r;
    return res;
}

void computeDP(int i,int jleft,int jright,int kleft,int kright)
{
    if(jleft > jright)return ;
    int jmid=(jleft + jright)>>1;
    int bestk=kleft;
    dp[i][jmid] = inf;
    for(int j=kleft;j < min(jmid,kright);++j)
        if(dp[i][jmid] > dp[i - 1][j] + calc(j + 1,min(jmid,kright)))
        {
            dp[i][jmid] = dp[i - 1][j] + res;
            bestk = j;
        }
    computeDP(i,jleft,jmid - 1,kleft,bestk);
    computeDP(i,jmid + 1,jright,bestk,kright);
}

void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    cur_l=0,cur_r=-1;
    res = 0;
    for(int i=1;i<=n;++i)
        dp[1][i] = calc(1,i);
    for(int i=2;i<=k;++i)
    {
        cur_l=0,cur_r=-1;
        res = 0;
        fill(cnt + 1,cnt + 1 + n,0);
        computeDP(i,1,n,1,n);
    }
    cout<<(dp[k][n] - n)/2;
}}

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(fn".inp","r",stdin);
    freopen(fn".out","w",stdout);
    #endif // ONLINE_JUDGE
//    ofstream cout(fn".inp");
//    cout<<100000<<' '<<20<<'\n';
//    for(int i=1;i<=100000;++i)
//        cout<<1 + rand() % 100000<<'\n';
//    cout.close();
    task::solve();
}


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 5, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];
bool vis[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r, id;
	bool operator<(const node &x)const {
		return l < x.l;
	}
}seg[N];

set<node>s;
vector<node>vec;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n; ++i) {
    	p[i] = i;
		seg[i].l = read(); seg[i].r = read();
		seg[i].id = i;
	}
	sort(seg + 1, seg + 1 + n);
	set<int>::iterator it;

	for (int i = 1; i <= n; ++i) {

		for (auto x:s) {
			if (x.r < seg[i].l) {
				vec.push_back(x);
				continue;
			}
			if (x.r < seg[i].r) {
				int a = x.id, b = seg[i].id;
				int pa = find(a), pb = find(b);
				cnt++;
				p[pa] = pb;
			}
		}
		for (auto x:vec) {
			s.erase(x);
		}
		vec.clear();
		s.insert(seg[i]);
	}

//	for (int i = 1; i <= n; ++i)
//		cout << p[i] << ' ';
//	puts("");

	if (cnt == n - 1) puts("YES");
	else puts("NO");

    // #ifndef ONLINE_JUDGE
    //     system("pause");
    // #endif
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r, id;
	bool operator<(const node &x) const {
		return l < x.l;
	}
}seg1[N];

struct node2 {
	int l, r, id;
	bool operator<(const node2 &x) const {
		return r < x.r;
	}
}seg2[N];

set<node2>s;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n + n; ++i) p[i] = i;
    for (int i = 1; i <= n; ++i) {
		seg1[i].l = read(); seg1[i].r = read();
		seg1[i].id = i;
	}
	sort(seg1 + 1, seg1 + 1 + n);

	s.insert({seg1[1].l, seg1[1].r, seg1[1].id});
	for (int i = 2; i <= n; ++i) {

		while (1) {
			if (s.size() == 0) break;
			node2 t = *s.begin();
			if (t.r < seg1[i].l) s.erase(*s.begin());
			else break;
		}

		for (auto x:s) {

			if (x.r >= seg1[i].r) break;

			if (x.r < seg1[i].r) {
				int a = x.id, b = seg1[i].id;
				int pa = find(a), pb = find(b);
				cnt++;
				p[pa] = pb;
			}

		}
		node2 tt = {seg1[i].l, seg1[i].r, seg1[i].id};
		s.insert(tt);
	}

	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r;
	bool operator<(const node &x) const {
		return l < x.l;
	}
}seg1[N];

set<node>s;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    set<node>::iterator it;

    for (int i = 1; i <= n; ++i) {
		seg1[i].l = read(); seg1[i].r = read();
		p[i] = i;
	}
	sort(seg1 + 1, seg1 + 1 + n);
	node t;
	t.l = seg1[1].r; t.r = 1;
	s.insert(t);

	for (int i = 2; i <= n; ++i) {

		while (1) {
			if (!s.size()) break;
			t = *s.begin();
			if (t.l <= seg1[i].l) s.erase(s.begin());
			else break;
		}

		for (auto x:s) {

			if (x.l >= seg1[i].r) break;

			int a = i, b = x.r;
//			if (find(a) != find(b)) p[p[a]] = p[b];
//			else {
//				puts("NO");
//				return 0;
//			}
			int pa = find(a), pb = find(b);
			if (pa != pb) p[pa] = pb;
			else {
				puts("NO");
				return 0;
			}
		}
		t.l = seg1[i].r, t.r = i;
		s.insert(t);
	}

	for (int i = 1; i <= n; ++i) if (i == find(i)) cnt++;

	if (cnt == 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r, id;
	bool operator<(const node &x) const {
		return l < x.l;
	}
}seg1[N];

struct node2 {
	int l, r, id;
	bool operator<(const node2 &x) const {
		return r < x.r;
	}
}seg2[N];

set<node2>s;
vector<node2>vec;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n + n; ++i) p[i] = i;
    for (int i = 1; i <= n; ++i) {
		seg1[i].l = read(); seg1[i].r = read();
		seg1[i].id = i;
	}
	sort(seg1 + 1, seg1 + 1 + n);

	for (int i = 1; i <= n; ++i) {

		for (auto x:s) {
			if (x.r < seg1[i].l) {
				vec.push_back(x);
				continue;
			}

			if (x.r < seg1[i].r) {
				int a = x.id, b = seg1[i].id;
				int pa = find(a), pb = find(b);
				cnt++;
				p[pa] = pb;
			} else break;
		}

		for (auto x:vec) {
			s.erase(x);
		}
		vec.clear();
		node2 tt = {seg1[i].l, seg1[i].r, seg1[i].id};
		s.insert(tt);
	}

	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r;
	bool operator<(const node &x) const {
		return l < x.l;
	}
}seg1[N];

set<node>s;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n; ++i) p[i] = i;
    for (int i = 1; i <= n; ++i) {
		seg1[i].l = read(); seg1[i].r = read();
	}
	sort(seg1 + 1, seg1 + 1 + n);
	node t;
	s.insert({seg1[1].r, 1});

	for (int i = 2; i <= n; ++i) {

		while (1) {
			if (!s.size()) break;
			t = *s.begin();
			if (t.l <= seg1[i].l) s.erase(s.begin());
			else break;
		}

		for (auto x:s) {

			if (x.l >= seg1[i].r) break;

			int a = i, b = t.r;
			int pa = find(a), pb = find(b);
			cnt++;
			p[pa] = pb;
		}
		t.l = seg1[i].r, t.r = i;
		s.insert(t);
	}

	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#define N 500005
using namespace std;
struct node{
	int l, r;
	bool operator < (const node & A) const{
		return l < A.l;
	}
}d[N];
set<node> s;
int f[N], cnt;
int find(int x){
	return x == f[x]? x: f[x] = find(f[x]);
}
int main(){
	int i, j, n, m, a, b;
	set<node>::iterator it;
	node t;
	scanf("%d", &n);
	for(i = 1; i <= n; i++){
		scanf("%d%d", &d[i].l, &d[i].r);
		f[i] = i;
	}
	sort(d + 1, d + i);
	t.l = d[1].r, t.r = 1;
	s.insert(t);
	for(i = 2; i <= n; i++){
		while(1){
			if(!s.size()) break;
			t = *s.begin();
			if(t.l <= d[i].l) s.erase(s.begin());
			else break;
		}
		for (auto x:s) {
//		for(it = s.begin(); it != s.end(); it++){
//			t = *it;
			auto t = x;
			if(t.l >= d[i].r) break;
			a = i, b = t.r;
			if(find(a) != find(b)) f[f[b]] = f[a];
			else{
				printf("NO");
				return 0;
			}
		}
		t.l = d[i].r, t.r = i;
		s.insert(t);
	}
	for(i = 1; i <= n; i++) if(i == find(i)) cnt++;
	if(cnt == 1) printf("YES");
	else printf("NO");
	return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r, id;
	bool operator<(const node &x) const {
		return l < x.l;
	}
}seg1[N];

struct node2 {
	int l, r, id;
	bool operator<(const node2 &x) const {
		return r < x.r;
	}
}seg2[N];

set<node2>s;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n + n; ++i) p[i] = i;
    for (int i = 1; i <= n; ++i) {
		seg1[i].l = read(); seg1[i].r = read();
		seg1[i].id = i;
	}
	sort(seg1 + 1, seg1 + 1 + n);

	s.insert({seg1[1].l, seg1[1].r, seg1[1].id});

	for (int i = 2; i <= n; ++i) {

		while (1) {
			if (s.size() == 0) break;
			node2 t = *s.begin();
			if (t.r <= seg1[i].l) s.erase(s.begin());
			else break;
		}

		for (auto x:s) {

			if (x.r >= seg1[i].r) break;

			int a = x.id, b = seg1[i].id;
			int pa = find(a), pb = find(b);
			cnt++;
			p[pa] = pb;
		}
		node2 tt = {seg1[i].l, seg1[i].r, seg1[i].id};
		s.insert(tt);
	}

	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r, id;
	bool operator<(const node &x)const {
		return l < x.l;
	}
}seg[N];

set<node>s;
vector<node>vec;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n + n; ++i) p[i] = i;
    for (int i = 1; i <= n; ++i) {
		seg[i].l = read(); seg[i].r = read();
		seg[i].id = i;
	}
	sort(seg + 1, seg + 1 + n);

	for (int i = 1; i <= n; ++i) {

		for (auto x:s) {
			if (x.r < seg[i].l) {
				vec.push_back(x);
				continue;
			}

			if (x.r < seg[i].r) {
				int a = x.id, b = seg[i].id;
				int pa = find(a), pb = find(b);
				cnt++;
				p[pa] = pb;
			}
		}
		for (auto x:vec) {
			s.erase(x);
		}
		vec.clear();
		s.insert(seg[i]);
	}

	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r, id;
	bool operator<(const node &x) const {
		return l < x.l;
	}
}seg1[N];

struct node2 {
	int l, r, id;
	bool operator<(const node2 &x) const {
		return r < x.r;
	}
}seg2[N];

set<node2>s;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n + n; ++i) p[i] = i;
    for (int i = 1; i <= n; ++i) {
		seg1[i].l = read(); seg1[i].r = read();
		seg1[i].id = i;
	}
	sort(seg1 + 1, seg1 + 1 + n);

	for (int i = 1; i <= n; ++i) {

		while (1) {
			if (s.size() == 0) break;
			node2 t = *s.begin();
			if (t.r <= seg1[i].l) s.erase(*s.begin());
			else break;
		}

		for (auto x:s) {

			if (x.l >= seg1[i].r) break;

			if (x.r < seg1[i].r) {
				int a = x.id, b = seg1[i].id;
				int pa = find(a), pb = find(b);
				cnt++;
				p[pa] = pb;
			}

		}
		node2 tt = {seg1[i].l, seg1[i].r, seg1[i].id};
		s.insert(tt);
	}
	//
	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r, id;
	bool operator<(const node &x) const {
		return l < x.l;
	}
}seg1[N];

struct node2 {
	int l, r, id;
	bool operator<(const node2 &x) const {
		return r < x.r;
	}
}seg2[N];

set<node2>s;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n + n; ++i) p[i] = i;
    for (int i = 1; i <= n; ++i) {
		seg1[i].l = read(); seg1[i].r = read();
		seg1[i].id = i;
	}
	sort(seg1 + 1, seg1 + 1 + n);

	for (int i = 1; i <= n; ++i) {

		while (1) {
			if (s.size() == 0) break;
			node2 t = *s.begin();
			if (t.r < seg1[i].l) s.erase(*s.begin());
			else break;
		}

		for (auto x:s) {
//			if (x.r < seg1[i].l) {
//				vec.push_back(x);
//				continue;
//			}

			if (x.r < seg1[i].r) {
				int a = x.id, b = seg1[i].id;
				int pa = find(a), pb = find(b);
				cnt++;
				p[pa] = pb;
			} else break;

		}
		node2 tt = {seg1[i].l, seg1[i].r, seg1[i].id};
		s.insert(tt);
	}

	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r, id;
	bool operator<(const node &x) const {
		return l < x.l;
	}
}seg1[N];

struct node2 {
	int l, r, id;
	bool operator<(const node2 &x) const {
		return r < x.r;
	}
}seg2[N];

set<node2>s;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n + n; ++i) p[i] = i;
    for (int i = 1; i <= n; ++i) {
		seg1[i].l = read(); seg1[i].r = read();
		seg1[i].id = i;
	}
	sort(seg1 + 1, seg1 + 1 + n);

	for (int i = 1; i <= n; ++i) {

		while (1) {
			if (s.size() == 0) break;
			node2 t = *s.begin();
			if (t.r < seg1[i].l) s.erase(*s.begin());
			else break;
		}

		for (auto x:s) {

			if (x.r >= seg1[i].r) break;

			if (x.r < seg1[i].r) {
				int a = x.id, b = seg1[i].id;
				int pa = find(a), pb = find(b);
				cnt++;
				p[pa] = pb;
			}

		}
		node2 tt = {seg1[i].l, seg1[i].r, seg1[i].id};
		s.insert(tt);
	}

	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r;
	bool operator<(const node &x) const {
		return l < x.l;
	}
}seg1[N];

set<node>s;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n; ++i) p[i] = i;
    for (int i = 1; i <= n; ++i) {
		seg1[i].l = read(); seg1[i].r = read();
	}
	sort(seg1 + 1, seg1 + 1 + n);
	node t;
	s.insert({seg1[1].r, 1});

	for (int i = 2; i <= n; ++i) {

		while (1) {
			if (!s.size()) break;
			t = *s.begin();
			if (t.l <= seg1[i].l) s.erase(s.begin());
			else break;
		}

		for (auto x:s) {

			if (x.l >= seg1[i].r) break;

			int a = i, b = x.r;
			int pa = find(a), pb = find(b);
			cnt++;
			p[pa] = pb;
		}
		t.l = seg1[i].r, t.r = i;
		s.insert(t);
	}

	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 100, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r, id;
	bool operator<(const node &x) const {
		return l < x.l;
	}
}seg1[N];

struct node2 {
	int l, r, id;
	bool operator<(const node2 &x) const {
		return r < x.r;
	}
}seg2[N];

set<node2>s;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n + n; ++i) p[i] = i;
    for (int i = 1; i <= n; ++i) {
		seg1[i].l = read(); seg1[i].r = read();
		seg1[i].id = i;
	}
	sort(seg1 + 1, seg1 + 1 + n);

	for (int i = 1; i <= n; ++i) {

		while (1) {
			if (s.size() == 0) break;
			node2 t = *s.begin();
			if (t.r < seg1[i].l) s.erase(*s.begin());
			else break;
		}

		for (auto x:s) {

			if (x.r >= seg[1].r) break;

			if (x.r < seg1[i].r) {
				int a = x.id, b = seg1[i].id;
				int pa = find(a), pb = find(b);
				cnt++;
				p[pa] = pb;
			}

		}
		node2 tt = {seg1[i].l, seg1[i].r, seg1[i].id};
		s.insert(tt);
	}

	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#include <set>
// #pragma GCC optimize(2)

//#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)
const ll LINF = 0x7fffffffffffffffll;

const int N = 1e6 + 5, M = 1e6 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, cnt;
int p[N];
bool vis[N];

int find(int x) {
	if (p[x] != x) return p[x] = find(p[x]);
	else return p[x];
}

inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

struct node {
	int l, r, id;
	bool operator<(const node &x)const {
		return l < x.l;
	}
}seg[N];

multiset<node>s;
vector<node>vec;

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    n = read();
    for (int i = 1; i <= n; ++i) {
    	p[i] = i;
		seg[i].l = read(); seg[i].r = read();
		seg[i].id = i;
	}
	sort(seg + 1, seg + 1 + n);
	set<int>::iterator it;

	for (int i = 1; i <= n; ++i) {

		for (auto x:s) {
			if (x.r < seg[i].l) {
				vec.push_back(x);
				continue;
			}
			if (x.r < seg[i].r) {
				int a = x.id, b = seg[i].id;
				int pa = find(a), pb = find(b);
				cnt++;
				p[pa] = pb;
			}
		}
		for (auto x:vec) {
			s.erase(x);
		}
		vec.clear();
		s.insert(seg[i]);
	}

	if (cnt == n - 1) puts("YES");
	else puts("NO");
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18
const int N=500010;

v g[N];
vbool visit(N);

void dfs(ll i, ll p=-1)
{
	visit[i]=1;
	//cout<<i+1<<" ";
	for(ll to: g[i])
		if(to!=p && !visit[to])
			dfs(to);
}

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
	ll n;
	cin>>n;
	vpair a(n), pts;

	fr(i, 0, n)
	{
		cin>>a[i].ft>>a[i].sd;
		pts.pb(make_pair(a[i].ft, i));
		pts.pb(make_pair(a[i].sd, i));
	}
	sort(all(pts));

	ll cnt=0;
	set<pll> open;
	for(auto it: pts)
	{
		if(cnt>=n) break;
		if(open.count(it)) open.erase(it);
		else
		{
			ll i=it.sd;
			ll end_pt=a[i].sd;
			for(auto jt: open)
			{
				if(jt.ft > end_pt) continue;
				int j=jt.sd;
				g[i].pb(j);
				g[j].pb(i);
				cnt++;
			}
			open.insert(make_pair(a[i].sd, i));
		}
	}
	dfs(0);
	if(cnt==n-1 && count(all(visit), 1)==n) cout<<"YES";
	else cout<<"NO";
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18
vecv g;
void solve()
{
	ll n;
	cin>>n;
	map<ll, ll> mfr, med;
	g.resize(n);
	vpair a(n), points;
	fr(i, 0, n) cin>>a[i].ft>>a[i].sd;
	sort(all(a));
	fr(i, 0, n)
	{
		points.pb(make_pair(a[i].ft, 0));
		points.pb(make_pair(a[i].sd, 1));
		mfr[a[i].ft]=i;
		med[a[i].sd]=i;
	}
	sort(all(points));
	set<ll> open;
	for(auto p: points)
	{
		if(p.sd==0)
			open.insert(mfr[p.ft]);
		else
		{
			for(ll i:open)
			{
				if(i<=med[p.ft]) continue;
				g[i].pb(med[p.ft]);
				g[med[p.ft]].pb(i);
			}
			open.erase(open.find(med[p.ft]));
		}
	}
	bool f=1;
	ll edges=0;
	fr(i, 0, n)
	{
		if(g[i].size()==0) f=0;
		edges+=g[i].size();
	}
	if(n==1) f=1;
	if(edges!=2*(n-1)) f=0;
	f?cout<<"YES\n":cout<<"NO\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cout<<fixed<<setprecision(0);
    //freopen("input.txt", "r", stdin);
    ll t=1; //cin>>t;
    while(t--) solve();
    return 0;
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18

const int N=500010;
ll n;
v g[N];
vbool visit(N);
pll a[N];

void dfs(ll i, ll p=-1)
{
	visit[i]=1;
	//cout<<i+1<<" ";
	for(ll to: g[i])
	{
		if(to!=p)
		{
			if(!visit[to]) dfs(to, i);
		}
	}

}

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

	cin>>n;
	vpair pts;
	fr(i, 0, n) cin>>a[i].ft>>a[i].sd;

	fr(i, 0, n)
	{
		pts.pb(make_pair(a[i].ft, i));
		pts.pb(make_pair(a[i].sd, i));
	}
	sort(all(pts));

	ll cnt=0;
	set<pll> open;
	for(auto it: pts)
	{
		if(cnt>=n) break;
		if(open.count(it)) open.erase(it);
		else
		{
			ll i=it.sd;
			ll end_pt=a[i].sd;
			for(auto jt: open)
			{
				if(jt.ft > end_pt) break;
				int j=jt.sd;
				g[i].pb(j);
				g[j].pb(i);
				cnt++;
				if(cnt>=n) break;
			}
			open.insert(make_pair(a[i].sd, i));

		}
	}
	dfs(0);
	if(cnt==n-1 && count(all(visit), 1)==n) cout<<"YES";
	else cout<<"NO";
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18
vecv g;
void solve()
{
	ll n;
	cin>>n;
	map<ll, ll> mfr, med;
	g.resize(n);
	vpair a(n), points;
	fr(i, 0, n) cin>>a[i].ft>>a[i].sd;
	sort(all(a));
	fr(i, 0, n)
	{
		points.pb(make_pair(a[i].ft, 0));
		points.pb(make_pair(a[i].sd, 1));
		mfr[a[i].ft]=i;
		med[a[i].sd]=i;
	}
	sort(all(points));
	set<ll> open;
	for(auto p: points)
	{
		if(p.sd==0)
			open.insert(mfr[p.ft]);
		else
		{
			for(ll i:open)
			{
				if(i<=med[p.ft]) continue;
				g[i].pb(med[p.ft]);
				g[med[p.ft]].pb(i);
			}
			open.erase(open.find(med[p.ft]));
		}
	}
	bool f=1;
	ll edges=0;
	fr(i, 0, n)
	{
		if(g[i].size()==0) f=0;
		edges+=g[i].size();
	}
	if(edges!=2*(n-1)) f=0;
	f?cout<<"YES\n":cout<<"NO\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cout<<fixed<<setprecision(0);
    //freopen("input.txt", "r", stdin);
    ll t=1; //cin>>t;
    while(t--) solve();
    return 0;
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18
vecv g;
void solve()
{
	ll n;
	cin>>n;
	map<ll, ll> mfr, med;
	g.resize(n);
	vpair a(n), points;
	fr(i, 0, n) cin>>a[i].ft>>a[i].sd;
	sort(all(a));
	fr(i, 0, n)
	{
		points.pb(make_pair(a[i].ft, 0));
		points.pb(make_pair(a[i].sd, 1));
		mfr[a[i].ft]=i;
		med[a[i].sd]=i;
	}
	sort(all(points));
	set<ll> open;
	ll inter=0;bool f=1;
	for(auto p: points)
	{
		if(p.sd==0)
			open.insert(mfr[p.ft]);
		else
		{
			inter+=distance(open.find(med[p.ft]), open.end())-1;
			open.erase(open.find(med[p.ft]));
		}
		if(inter>n) {f=0;break;}
	}
	if(!f)
	{
		cout<<"NO\n";
		return;
	}
	/*ll edges=0;
	fr(i, 0, n)
	{
		if(g[i].size()==0) f=0;
		edges+=g[i].size();
	}*/
	if(n==1) f=1;
	if(inter!=(n-1)) f=0;
	f?cout<<"YES\n":cout<<"NO\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cout<<fixed<<setprecision(0);
    //freopen("input.txt", "r", stdin);
    ll t=1; //cin>>t;
    while(t--) solve();
    return 0;
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18

const int N=500010;
ll n;
v g[N];
vbool visit(N);
pll a[N];

void dfs(ll i, ll p=-1)
{
	visit[i]=1;
	//cout<<i+1<<" ";
	for(ll to: g[i])
	{
		if(to!=p)
		{
			if(!visit[to]) dfs(to, i);
		}
	}

}

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

	cin>>n;
	vpair pts;
	fr(i, 0, n) cin>>a[i].ft>>a[i].sd;

	fr(i, 0, n)
	{
		pts.pb(make_pair(a[i].ft, i));
		pts.pb(make_pair(a[i].sd, i));
	}
	sort(all(pts));

	ll cnt=0;
	set<pll> open;
	for(auto it: pts)
	{
		if(cnt>=n) break;
		if(open.count(it)) open.erase(it);
		else
		{
			ll i=it.sd;
			ll end_pt=a[i].sd;
			for(auto jt: open)
			{
				if(jt.ft > end_pt) continue;
				int j=jt.sd;
				g[i].pb(j);
				g[j].pb(i);
				cnt++;
				if(cnt>=n) break;
			}
			open.insert(make_pair(a[i].sd, i));

		}
	}
	dfs(0);
	if(cnt==n-1 && count(all(visit), 1)==n) cout<<"YES";
	else cout<<"NO";
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18
vecv g;
vbool visit;

void dfs(ll i)
{
	if(visit[i]) return;
	visit[i]=1;
	//cout<<i+1<<" ";
	for(ll to: g[i])
		dfs(to);
}

int main()
{
	ll n;
	cin>>n;
	vpair a(n), pts;
	g.resize(n); visit.resize(n, 0);

	fr(i, 0, n)
	{
		cin>>a[i].ft>>a[i].sd;
		pts.pb(make_pair(a[i].ft, i));
		pts.pb(make_pair(a[i].sd, i));
	}
	sort(all(pts));

	ll cnt=0;
	set<pll> open;
	for(auto it: pts)
	{
		if(cnt>=n) break;
		if(open.count(it)) open.erase(it);
		else
		{
			ll i=it.sd;
			ll end_pt=a[i].sd;
			for(auto jt: open)
			{
				if(jt.ft > end_pt) continue;
				int j=jt.sd;
				g[i].pb(j);
				g[j].pb(i);
				cnt++;
			}
			open.insert(make_pair(a[i].sd, i));
		}
	}
	dfs(0);
	if(cnt==n-1 && count(all(visit), 1)==n) cout<<"YES";
	else cout<<"NO";
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18
const int N=500010;

v g[N];
vbool visit(N);

void dfs(ll i)
{
	if(visit[i]) return;
	visit[i]=1;
	//cout<<i+1<<" ";
	for(ll to: g[i])
		dfs(to);
}

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
	ll n;
	cin>>n;
	vpair a(n), pts;

	fr(i, 0, n)
	{
		cin>>a[i].ft>>a[i].sd;
		pts.pb(make_pair(a[i].ft, i));
		pts.pb(make_pair(a[i].sd, i));
	}
	sort(all(pts));

	ll cnt=0;
	set<pll> open;
	for(auto it: pts)
	{
		if(cnt>=n) break;
		if(open.count(it)) open.erase(it);
		else
		{
			ll i=it.sd;
			ll end_pt=a[i].sd;
			for(auto jt: open)
			{
				if(jt.ft > end_pt) continue;
				int j=jt.sd;
				g[i].pb(j);
				g[j].pb(i);
				cnt++;
			}
			open.insert(make_pair(a[i].sd, i));
		}
	}
	dfs(0);
	if(cnt==n-1 && count(all(visit), 1)==n) cout<<"YES";
	else cout<<"NO";
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18
vecv g;
vbool visit;

void dfs(ll i, ll p=-1)
{
	visit[i]=1;
	//cout<<i+1<<" ";
	for(ll to: g[i])
	{
		if(to!=p)
		{
			if(!visit[to]) dfs(to, i);
		}
	}

}
void solve()
{
	ll n;
	cin>>n;
	map<ll, ll> mfr, med;
	g.resize(n); visit.resize(n);
	vpair a(n), points;
	fr(i, 0, n) cin>>a[i].ft>>a[i].sd;
	sort(all(a));
	fr(i, 0, n)
	{
		points.pb(make_pair(a[i].ft, 0));
		points.pb(make_pair(a[i].sd, 1));
		mfr[a[i].ft]=i;
		med[a[i].sd]=i;
	}
	sort(all(points));
	set<ll> open;
	ll cnt=0;
	for(auto p: points)
	{
		if(cnt>=n) break;
		if(p.sd==0)
			open.insert(mfr[p.ft]);
		else
		{
			set<ll>::iterator it=open.find(med[p.ft]);
			it++;
			for(it; it!=open.end(); it++)
			{
				//if(i<=med[p.ft]) continue;
				ll i=*it;
				g[i].pb(med[p.ft]);
				g[med[p.ft]].pb(i);
				cnt++;
				if(cnt>=n) break;
			}
			open.erase(open.find(med[p.ft]));
		}
	}
	dfs(0);
	if(cnt==n-1 && count(all(visit), 1)==n) cout<<"YES";
	else cout<<"NO";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cout<<fixed<<setprecision(0);
    //freopen("input.txt", "r", stdin);
    ll t=1; //cin>>t;
    while(t--) solve();
    return 0;
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18
vecv g;
void solve()
{
	ll n;
	cin>>n;
	map<ll, ll> mfr, med;
	g.resize(n);
	vpair a(n), points;
	fr(i, 0, n) cin>>a[i].ft>>a[i].sd;
	sort(all(a));
	fr(i, 0, n)
	{
		points.pb(make_pair(a[i].ft, 0));
		points.pb(make_pair(a[i].sd, 1));
		mfr[a[i].ft]=i;
		med[a[i].sd]=i;
	}
	sort(all(points));
	set<ll> open;
	ll inter=0;bool f=1;
	for(auto p: points)
	{
		if(p.sd==0)
			open.insert(mfr[p.ft]);
		else
		{
			for(ll i:open)
			{
				if(i<=med[p.ft]) continue;
				g[i].pb(med[p.ft]);
				g[med[p.ft]].pb(i);
				inter++;
			}
			open.erase(open.find(med[p.ft]));
		}
		if(inter>n) {f=0;break;}
	}
	if(!f)
	{
		cout<<"NO\n";
		return;
	}
	ll edges=0;
	fr(i, 0, n)
	{
		if(g[i].size()==0) f=0;
		edges+=g[i].size();
	}
	if(n==1) f=1;
	if(edges!=2*(n-1)) f=0;
	f?cout<<"YES\n":cout<<"NO\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cout<<fixed<<setprecision(0);
    //freopen("input.txt", "r", stdin);
    ll t=1; //cin>>t;
    while(t--) solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define forn(i, n) for (int i = 0; i < int(n); i++)

typedef long long li;
typedef pair<li, li> pt;

const int N = 500010;

int n;
pt a[N];
vector<int> g[N];
bool used[N];

void dfs(int v, int p = -1) {
	used[v] = true;
	for (auto to : g[v]) if (to != p) {
		if (!used[to])
			dfs(to, v);
	}
}

int main() {
	scanf("%d", &n);
	forn(i, n) scanf("%d%d", &a[i].x, &a[i].y);

	vector<pt> evs;
	forn(i, n) {
		evs.pb(mp(a[i].x, i));
		evs.pb(mp(a[i].y, i));
	}

	sort(all(evs));

	int cnt = 0;
	set<pt> cur;
	for (auto it : evs) {
		if (cnt >= n) break;
		if (cur.count(it)) {
			cur.erase(it);
		} else {
			int i = it.y;
			int r = a[i].y;
			for (auto jt : cur) {
				if (jt.x > r) break;
				int j = jt.y;
				g[i].pb(j);
				g[j].pb(i);
				cnt++;
				if (cnt >= n) break;
			}
			cur.insert(mp(a[i].y, i));
		}
	}

	dfs(0);
	puts(cnt == n - 1 && count(used, used + n, 1) == n ? "YES" : "NO");
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18
vecv g;
void solve()
{
	ll n;
	cin>>n;
	map<ll, ll> mfr, med;
	g.resize(n);
	vpair a(n), points;
	fr(i, 0, n) cin>>a[i].ft>>a[i].sd;
	sort(all(a));
	fr(i, 0, n)
	{
		points.pb(make_pair(a[i].ft, 0));
		points.pb(make_pair(a[i].sd, 1));
		mfr[a[i].ft]=i;
		med[a[i].sd]=i;
	}
	sort(all(points));
	set<ll> open;
	ll inter=0;bool f=1;
	for(auto p: points)
	{
		if(p.sd==0)
			open.insert(mfr[p.ft]);
		else
		{
			for(ll i:open)
			{
				if(i<=med[p.ft]) continue;
				/*g[i].pb(med[p.ft]);
				g[med[p.ft]].pb(i);*/
				inter++;
			}
			open.erase(open.find(med[p.ft]));
		}
		if(inter>n) {f=0;break;}
	}
	if(!f)
	{
		cout<<"NO\n";
		return;
	}
	/*ll edges=0;
	fr(i, 0, n)
	{
		if(g[i].size()==0) f=0;
		edges+=g[i].size();
	}*/
	if(n==1) f=1;
	if(inter!=(n-1)) f=0;
	f?cout<<"YES\n":cout<<"NO\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cout<<fixed<<setprecision(0);
    //freopen("input.txt", "r", stdin);
    ll t=1; //cin>>t;
    while(t--) solve();
    return 0;
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18
const int N=500010;

v g[N];
bool visit[N];

void dfs(ll i)
{
	if(visit[i]) return;
	visit[i]=1;
	//cout<<i+1<<" ";
	for(ll to: g[i])
		dfs(to);
}

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
	ll n;
	cin>>n;
	vpair a(n), pts;

	fr(i, 0, n)
	{
		cin>>a[i].ft>>a[i].sd;
		pts.pb(make_pair(a[i].ft, i));
		pts.pb(make_pair(a[i].sd, i));
	}
	sort(all(pts));

	ll cnt=0;
	set<pll> open;
	for(auto it: pts)
	{
		if(cnt>=n) break;
		if(open.count(it)) open.erase(it);
		else
		{
			ll i=it.sd;
			ll end_pt=a[i].sd;
			for(auto jt: open)
			{
				if(jt.ft > end_pt) continue;
				int j=jt.sd;
				g[i].pb(j);
				g[j].pb(i);
				cnt++;
			}
			open.insert(make_pair(a[i].sd, i));
		}
	}
	dfs(0);
	if(cnt==n-1 && count(all(visit), 1)==n) cout<<"YES";
	else cout<<"NO";
}


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

#define ll long long
#define ld long double
#define ft first
#define sd second

#define vint vector<int>
#define vlong vector<long>
#define vbool vector<bool>
#define v vector<ll>
#define vecv vector<v>
#define pll pair<ll, ll>
#define vpair vector<pll>

#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))

#define err() cout<<"==================================\n";
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n"
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n"
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n"
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n"

#define pb push_back
#define fr(i, a, b) for(ll i=a; i<b; i++)
#define rf(i, a, b) for(ll i=a; i>=b; i--)

#define MOD 1e9 + 7
#define INF 9e18

const int N=500010;
ll n;
v g[N];
vbool visit(N);
pll a[N];

void dfs(ll i, ll p=-1)
{
	visit[i]=1;
	//cout<<i+1<<" ";
	for(ll to: g[i])
	{
		if(to!=p)
		{
			if(!visit[to]) dfs(to, i);
		}
	}

}

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

	cin>>n;
	vpair pts;
	fr(i, 0, n) cin>>a[i].ft>>a[i].sd;

	fr(i, 0, n)
	{
		pts.pb(make_pair(a[i].ft, i));
		pts.pb(make_pair(a[i].sd, i));
	}
	sort(all(pts));

	ll cnt=0;
	set<pll> open;
	for(auto it: pts)
	{
		if(cnt>=n) break;
		if(open.count(it)) open.erase(it);
		else
		{
			ll i=it.sd;
			ll end_pt=a[i].sd;
			for(auto jt: open)
			{
				if(jt.ft > end_pt) continue;
				int j=jt.sd;
				g[i].pb(j);
				g[j].pb(i);
				cnt++;
			}
			open.insert(make_pair(a[i].sd, i));
			if(cnt>=n) break;
		}
	}
	dfs(0);
	if(cnt==n-1 && count(all(visit), 1)==n) cout<<"YES";
	else cout<<"NO";
}