vj题目链接:https://cn.vjudge.net/contest/291737#problem/F
cf题目链接:https://codeforces.com/problemset/problem/612/D
思路:给出区间【l,r】相当于l开始后面全部加一,r开始后面全部减一,使用结构体,记录一下,把每个点的权计算计算出来。
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<vector>
using namespace std;
const int M=1e6;
vector<pair<int,int> >q;
struct node
{
int x,k;
bool operator < (const node a)
{
if(this->x==a.x)
return this->k>a.k;
return this->x<a.x;
}
}A[2*M];
int n,m,l,r;
int main()
{
while(~scanf("%d%d",&n,&m))
{
q.clear();
for(int i=0;i<n;i++)
{
scanf("%d%d",&l,&r);
A[2*i].x=l;
A[2*i].k=1;
A[2*i+1].x=r;
A[2*i+1].k=-1;
}
sort(A,A+2*n);
for(int i=1;i<2*n;i++) A[i].k+=A[i-1].k;
for(int i=0;i<2*n;i++)
{
if(A[i].k>=m)
{
l=A[i].x;
while(A[i].k>=m)i++;
r=A[i].x;
q.push_back(make_pair(l,r));
}
}
printf("%d\n",q.size());
for(int i=0;i<q.size();i++)
printf("%d %d\n",q[i].first,q[i].second);
}
}