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 satisfiedpoints and no others.
InputThe 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.
OutputFirst 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.
Examples3 2 0 5 -3 2 3 8
2 0 2 3 5
3 2 0 5 -3 3 3 8
1 0 5
题意:给n个区间求被覆盖次数>=k的区间有几个,并输出这些区间;
思路:将每次区间分成两部分,并且对区间的左右范围进行标记,然后进行排序,遇见left就ans+1,遇见right就ans-1
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define ll long long
#define memset(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXN=2000000+10;
//const int MAXM=100000+10;
const int inf=0x3f3f3f3f;
struct node
{
int val;
int dir;
} z[MAXN];
int cmp(node a,node b)
{
if(a.val!=b.val)
return a.val<b.val;
return a.dir<b.dir;
}
int x[MAXN];
int n,m;
int main()
{
while(~scanf("%d%d",&n,&m))
{
int left,right;
int k=0;
for(int i=0; i<n; i++)
{
scanf("%d%d",&left,&right);
z[k].val=left;
z[k++].dir=0;
z[k].val=right;
z[k++].dir=1;
}
sort(z,z+k,cmp);
int ans=0,sum=0;
for(int i=0; i<k; i++)
{
if(!z[i].dir)//遇见left
{
ans++;
if(ans==m)//不能写>=m,>m的区间包含在=m的区间
x[sum++]=z[i].val;
}
else
{
if(ans==m)
x[sum++]=z[i].val;
ans--;
}
}
printf("%d\n",sum/2);
for(int i=0; i<sum; i+=2)
{
printf("%d %d\n",x[i],x[i+1]);
}
}
}