codeforces 612D The Union of k-Segments (线段排序)
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.
The 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.
First 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.
3 2
0 5
-3 2
3 8
2
0 2
3 5
3 2
0 5
-3 3
3 8
1
0 5
题意:问这些线段覆盖的不小于k次的线段有哪些,注意有可能是一个点;
思路:把起点和终点标记后排序,用一个计数器当等于k事说明符合条件线段的起点或终点,加入容器中,注意排序的时候如果坐标相同一定是起点在终点前,不然点的情况会漏掉,最后还要把可以合并的区间合并;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int a,b;
struct node
{
int fi,se;
};
node po[2*N];
int cmp(node x,node y)
{
if(x.fi==y.fi)return x.se<y.se;
return x.fi<y.fi;
}
vector < int >ve;
int ans[2*N];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i =0;i<2*n;i+=2)
{
scanf("%d%d",&po[i].fi,&po[i+1].fi);
po[i].se=-1;
po[i+1].se=1;
}
sort(po,po+2*n,cmp);
int num=0;
for(int i=0;i<2*n;i++)
{
if(po[i].se==-1)
{
num++;