You are given n segments on the coordinate axis Ox and the number k. The point is satisfied if it belongs to at least k segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others.
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106) — the number of segments and the value of k.
The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109) each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order.
First line contains integer m — the smallest number of segments.
Next m lines contain two integers aj, bj (aj ≤ bj) — the ends of j-th segment in the answer. The segments should be listed in the order from left to right.
3 2 0 5 -3 2 3 8
2 0 2 3 5
3 2 0 5 -3 3 3 8
1 0 5
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#define maxn 2000010
using namespace std;
struct node
{
int val,f;
} num[maxn];
int ans[maxn];
bool cmp(node a,node b)
{
if(a.val!=b.val)return a.val<b.val;
else
{
return a.f>b.f;
}
}
int main()
{
int i,n,k,t=0;
scanf("%d%d",&n,&k);
for(i=1; i<=n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
num[t].val=a;
num[t++].f=1;
num[t].val=b;
num[t++].f=0;
}
sort(num,num+t,cmp);
int pk=0,flag=0,ansn=0;
for(i=0; i<t; i++)
{
if(num[i].f==1)
{
pk++;
if(pk==k)
{
ans[ansn]=num[i].val;
//printf("%d\n",ansi[ansn]);
ansn++;
}
}
else if(num[i].f==0)
{
if(pk==k)
{
ans[ansn]=num[i].val;
//printf("%d\n",ansj[ansn]);
ansn++;
}
pk--;
}
}
printf("%d\n",ansn/2);
for(i=0; i<ansn; i++)
{
printf("%d ",ans[i]);
i++;
printf("%d\n",ans[i]);
}
return 0;
}