您的位置:首页 > 其它

【codefores 612】 The Union of k-Segments 【思维+ 线段处理】

2017-09-19 18:17 459 查看
D. The Union of k-Segments

time limit per test4 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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 2

0 5

-3 2

3 8

output

2

0 2

3 5

input

3 2

0 5

-3 3

3 8

output

1

0 5

写这道题 之前是先写了 这道题 C 题

都是对 线段问题的处理 ,这种处理方式可以 记录一下。

处理完之后,找一下规律就知道,cnt表示以当前点为起点之后的点被线段的重叠个数。 之后枚举所有端点,一个左端点cnt++,一个右端点cnt–。 同时记录一下 最后答案的左点和右点就行。

代码

#include<bits/stdc++.h>
using namespace std;
#define _int64 LL

const int MAXN = 1e6+10;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct Node{
int p,t;
Node(){}
Node (int a,int b)  {  p=a; t=b; }
bool operator<(Node b){
if(b.p==p) return t<b.t;
else return p<b.p;
}
}P[MAXN*2];

vector < pair<int,int> > ve ;
int main(){
int n,k;scanf("%d%d",&n,&k);
int a,b,c;int tot=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&a,&b);
P[tot++]={a,-1};
P[tot++]={b,1};
}
sort(P,P+tot);
int l,r;int cnt=0;
for(int i=0;i<tot;i++){
if(P[i].t==-1) {
cnt++;
if(cnt==k) l=P[i].p;
}else {
cnt--;
if(cnt==k-1) {
r=P[i].p;
ve.push_back({l,r});
}
}
}
printf("%d\n",ve.size());
for(int i=0;i<ve.size();i++)
printf("%d %d\n",ve[i].first,ve[i].second);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: