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.
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次的区间(两端连续区间可以合并)最少有多少个,并输出
解 :先把线段按左端点由小到大排序,再把他们放入优先队列,以线段右端点小的在上面,如果队列里面有k个,那么计算这k个的重叠部分,大于k个的时候,把最上面的取出到只有k个,再计算 ,显然重叠部分的左端点为最后放进去的线段的左端点,因为最后放进去左端点值最大,重叠部分右端点为队列最上面的线段右端点值,如果左端点大于右端点说明没有重叠,有所有重叠部分后 ,排序合并输出
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
struct node
{
ll x,y;
friend bool operator < (node n1,node n2)
{
return n2.y<n1.y;
}
}e[1100000];
int cmp(node n1,node n2)
{
return n1.x<n2.x;
}
struct no
{
ll l,r;
}aa[100000],bb[100000];
int cmp1(no n1,no n2)
{
if(n1.l==n2.l)
return n1.r<n2.r;
return n1.l<n2.l;
}
int n,k;
int main()
{
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
scanf("%lld%lld",&e[i].x,&e[i].y);
sort(e,e+n,cmp);
priority_queue<node>q;
int tot=0,num=0;
for(int i=0;i<n;i++)
{
ll l=e[i].x,r;
q.push(e[i]);
tot++;
if(tot>k)
{
q.pop();
tot--;
}
if(tot==k)
{
node u=q.top();
r=u.y;
if(r>=l)
{
aa[num].l=l;
aa[num++].r=r;
}
}
}
sort(aa,aa+num,cmp1);
if(num==0)
{
printf("0\n");
return 0;
}
int numm=0;
ll l=aa[0].l,r=aa[0].r;
for(int i=1;i<num;i++)
{
if(aa[i].l<=r)
r=aa[i].r;
else
{
bb[numm].l=l;
bb[numm++].r=r;
l=aa[i].l;
r=aa[i].r;
}
}
bb[numm].l=l;
bb[numm++].r=r;
printf("%d\n",numm);
for(int i=0;i<numm;i++)
printf("%lld %lld\n",bb[i].l,bb[i].r);
}