CodeForces

来源:互联网 发布:中国人民航空大学知乎 编辑:程序博客网 时间:2024/05/23 01:57

D. The Union of k-Segments
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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.

Examples
input
3 20 5-3 23 8
output
20 23 5
input
3 20 5-3 33 8
output
10 5

题意:n条区间,求区间重叠次数超过k的区间最少有多少个

左端点赋1,右端点赋-1,全部整合在一起排个序,扫一遍,把所有重叠次数超过k的区间找出来,最后在扫一遍把可以连接在一起的区间连接起来。

会出现左右端点相同的区间。


#include<bits/stdc++.h>#define inf 0x3f3f3f3fusing namespace std;struct node{int index,flog;}num[2000005];int cmp(node u,node  v){if(u.index==v.index)return u.flog>v.flog;return u.index<v.index;}vector<node>ans,ans2;int main(){int n,a,b,i,k;cin>>n>>k;for(i=0;i<n;i++){scanf("%d %d",&a,&b);node t;t.index=a;t.flog=1;num[2*i]=t;t.index=b;t.flog=-1;num[2*i|1]=t;}sort(num,num+2*n,cmp);int now=0,index=inf;for(i=0;i<2*n;i++){now+=num[i].flog;if(now==k){if(index==inf){index=num[i].index;}}if(index!=inf&&now==k-1){node t;t.index=index;t.flog=num[i].index;ans.push_back(t);index=inf;}}if(ans.size()==0)    {        cout<<0<<endl;        return 0;    }    node now_=ans[0];for(i=1;i<ans.size();i++)    {        if(ans[i].index==now_.flog)        {            now_.flog=ans[i].flog;        }        else        {            ans2.push_back(now_);            now_=ans[i];        }    }    ans2.push_back(now_);cout<<ans2.size()<<endl;for(i=0;i<ans2.size();i++){printf("%d %d\n",ans2[i].index,ans2[i].flog);}return 0;}