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<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<vector>
#define LL __int64
using namespace std;
pair<int,int>line[3000006];
int cnt;
int t[3000006];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
cnt=1;
for(i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
line[cnt++]=make_pair(x,-1);
line[cnt++]=make_pair(y,1);
}
sort(line+1,line+cnt);
memset(t,0,sizeof(t));
vector<int>ans1;
vector<int>ans2;
for(i=1;i<cnt;i++)
{
t[i]=t[i-1]-line[i].second;
if(t[i]==m&&t[i-1]==m-1)
ans1.push_back(line[i].first);
}
memset(t,0,sizeof(t));
for(i=1;i<cnt;i++)
{
t[i]=t[i-1]-line[i].second;
if(t[i]==m-1&&t[i-1]==m)
ans2.push_back(line[i].first);
}
printf("%d\n",ans1.size());
for(i=0;i<ans1.size();i++)
{
printf("%d %d\n",ans1[i],ans2[i]);
}
}
}