D. The Union of k-Segments
 

You re 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.

Sample test(s)
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

 题意:给你一n条线段,一个k,问你有k跳线段以上相交的线段有多少

题解:按照 左右端点区分,x,y一起排序,找增加至k个左端点加入左答案,减少到k-1个端点加入右答案,注意一个点的线段

//meek
///#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** const int N=+;
const ll INF = 1ll<<;
const int inf = ;
const int mod= ; int n,k,x,y,t[N];
pair<int,int> Line[N];
vector<int >ans1,ans2;
int main() {
scanf("%d%d",&n,&k);
int cnt = ;
for(int i=;i<=n;i++) {
scanf("%d%d",&x,&y);
Line[cnt++] = MP(x,-);
Line[cnt++] = MP(y,);
}
sort(Line+,Line+cnt);
for(int i=;i<cnt;i++)
{
t[i] = t[i-] - Line[i].se;
if(t[i]==k&&t[i-]==k-)
ans1.pb(Line[i].fi);
}
memset(t,,sizeof(t));
for(int i=;i<cnt;i++)
{
t[i] = t[i-] - Line[i].se;
if(t[i]==k-&&t[i-]==k)
ans2.pb(Line[i].fi);
}
if(ans1.size()!=ans2.size()) ans2.pb(Line[cnt-].fi);
cout<<ans1.size()<<endl;
for(int i=;i<ans1.size();i++)
cout<<ans1[i]<<" "<<ans2[i]<<endl;
return ;
}

代码

Educational Codeforces Round 4 D. The Union of k-Segments 排序的更多相关文章

  1. 【扫描线】Educational Codeforces Round 4 D. The Union of k-Segments

    http://codeforces.com/contest/612/problem/D [题解] http://blog.csdn.net/strokess/article/details/52248 ...

  2. Educational Codeforces Round 5

    616A - Comparing Two Long Integers    20171121 直接暴力莽就好了...没什么好说的 #include<stdlib.h> #include&l ...

  3. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  4. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  5. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  6. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  7. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  8. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  9. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

随机推荐

  1. virtualbox下 ubuntu 14.04设置外网独立IP

    安装时记得选择sshserver vim /etc/network/interfaces iface eth0 inet static address YOUR IP netmask 子网掩码 get ...

  2. iOS学习之C语言分支结构

    一.BOOL类型 返回值:真:YES   假:NO 定义一个布尔类型的变量 YES == 1, NO == 0 计算机在识别时,YES就替换成1,NO就替换成0 BOOL isGirl = YES; ...

  3. 魔兽世界私服Trinity,从源码开始

    缘起因由 在一个无所事事的周末下午,突然想起魔兽世界,官方的账号很久没有上了,里面的大小号现在连满级都不是.以前曾经搭过传奇和星际争霸战网的私服自娱自乐,也听说过魔兽世界有开源的服务端模拟,既然兴致来 ...

  4. [CentOS 6.5 X64]讓firefox java plugin 啟動

    到ORACLE下載JRE http://www.oracle.com/technetwork/java/javase/downloads/index.html 我是X64所以下載 jre-7-linu ...

  5. iOS 数据库持久化

    Java代码 -(void) addObserver{ //当程序进入后台时执行操作 UIApplication *app = [UIApplication sharedApplication]; [ ...

  6. 混合使用C和C++

    C++作为C语言的扩展集,几乎所有的C程序都可以在C++中编译和运行,但是要注意C程序中可能使用了C++中的关键字作为变量,比如在C中:int class = 0; 但这在C++中不行.出于方便性,我 ...

  7. PB中获取datawindow提交的sql语句

    PB的群里边,有人问的到这个问题,查了一下,综合了两条回答,得到了答案 1.DW 控件的SQLpreview 事件里的sqlsyntax 参数即是 2.pb一般使用占位符优化SQL语句,也就是你看到的 ...

  8. webpack对样式的处理

    原文地址:https://github.com/zhengweikeng/blog/issues/9 我们可以在js中引入样式文件 require('myStyle.css') 这时我们便需要引入相应 ...

  9. Ionic入门一:Hello Ionic

    1.在终端里面进入准备存放App的目录:  2.Ionic官网提供了三个项目模板blank.tabs和sideMenu ,用“ionic start myApp tabs”创建ionic项目:  ...

  10. Careercup - Google面试题 - 4699414551592960

    2014-05-06 13:34 题目链接 原题: we have a random list of people. each person knows his own height and the ...