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. poj 1862 Stripies/优先队列

    原题链接:http://poj.org/problem?id=1862 简单题,贪心+优先队列主要练习一下stl大根堆 写了几种实现方式写成类的形式还是要慢一些... 手打的heap: 1: #inc ...

  2. 代码文档生成工具-Doxygen生成CHM和RTF图文教程

    Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统,可以从一套归档源文件开始,生成chm格式的文档.本文主要讲解如何在winddows下安装doxygen.     1.下载do ...

  3. c++ 性能

    http://blog.sina.com.cn/s/blog_4a471ff601013vud.html http://www.linuxidc.com/Linux/2015-06/118874.ht ...

  4. 为什么学习Python

    因为做iOS开发的,之前一直用OC,但是突然有一天苹果说出Swift,但是那时候的Swift真的是Bug多多,语法都不固定,所以只是大致看了看,而一年多之后,Swift已经发布2.0了,语言也相对稳定 ...

  5. 时隔3年半Spring.NET 2.0终于正式Release了

    一直很喜欢Spring.NET,不过2011年8月2日1.3.2正式release之后,再没有正式版本的release了. 直到4天前,Spring.NET 2.0 GA终于Release. http ...

  6. QT实现软件重启

    //重启软件 void MainWindow::on_pushButton_UI_reboot_clicked() { //方式1 需要主函数中事件循环判断 //qApp->exit(773); ...

  7. python三级菜单

    #-*- coding:utf-8 -*-#Author:gxli #一级菜单项def menu(): #遍历字典dic一级菜单 print('-----------一级菜单------------- ...

  8. 子网划分与CIDR(斜杠加数字的表示与IP 的关系)(改进)

    子网和CIDR   将常规的子网掩码转换为二进制,将发现子网掩格式为连续的二进制1跟连续0,其中子网掩码中为1的部份表示网络ID,子网掩中为0的表示主机ID.比如255.255.0.0转换为二进制为1 ...

  9. Android -- 创建桌面快捷方式

    代码                                                                                    /** * * 返回添加到桌 ...

  10. Notes of the scrum meeting(11/4)

    meeting time:20:00~20:30p.m.,November 4th,2013 meeting place:20号公寓楼前 attendees: 顾育豪                  ...