题目链接:http://codeforces.com/problemset/problem/612/D
题目大意:有 n 条线断,问被任意 k 条线段覆盖的区间有多少个并输出;
思路:把每个左端点标记为0,右端点标记为1,按值从小到大排序,值相同的标记为0的在前面;
定义 cut 为当前被多少线段覆盖,遇到标记为0的 cut++,当cut==k时,就是答案区间的左端点,
遇到标记为1 就cut--,当cut==k-1,就是答案区间的右端点,用个数组保存就行了;
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<math.h>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const double pi=acos(-1.0);
const int mod=1e9;
const int N=2e6+10;
struct node
{
int a,f;
} pos[N];
int ans[N][2];
bool cmp(node x,node y)
{
if(x.a!=y.a)
return x.a<y.a;
return x.f<y.f;
}
int main()
{
int n,k;
scanf("%d %d",&n,&k);
for(int i=0; i<n; i++)
{
scanf("%d %d",&pos[i].a,&pos[i+n].a);
pos[i].f=0;
pos[i+n].f=1;
}
sort(pos,pos+2*n,cmp);
int cut=0,e=0;
for(int i=0; i<2*n; i++)
{
if(pos[i].f==0)
{
cut++;
if(cut==k)
ans[e][0]=pos[i].a;
}
else
{
cut--;
if(cut==k-1)
ans[e++][1]=pos[i].a;
}
}
printf("%d\n",e);
for(int i=0; i<e; i++)
printf("%d %d\n",ans[i][0],ans[i][1]);
return 0;
}