题意是给你很多个区间,让你找覆盖恰好为k层的区间。
用扫描线直接做就行了。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
#define LL long long int
int n, k;
int a, b;
vector<int>vec;
struct node
{
int x, op;
}s[2000100];
bool CMP(node a, node b)
{
if (a.x == b.x)
return a.op > b.op;
return a.x < b.x;
}
int main()
{
scanf("%d%d", &n, &k);
int cmp = 0;
for (int i = 0;i < n;i++)
{
scanf("%d%d", &a, &b);
s[cmp].x = a;
s[cmp++].op = 1;
s[cmp].x = b;
s[cmp++].op = -1;
}
sort(s, s + cmp, CMP);
int cnt = 0;
for (int i = 0;i < cmp;i++)
{
if (s[i].op == 1)
{
cnt++;
if (cnt == k)
vec.push_back(s[i].x);
}
else
{
if (cnt == k)
vec.push_back(s[i].x);
cnt--;
}
}
printf("%d\n", vec.size()/2);
for (int i = 0;i < vec.size()/2;i++)
printf("%d %d\n", vec[i << 1], vec[i << 1 | 1]);
return 0;
}