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 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次的区间。

题解:用线段扫描左端点(用-1标记)进入tmp++,右端点(用1标记)出来tmp--,当tmp为k时就是答案区间。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
using namespace std;
vector<int>ans;
vector<pii>s;
int n,k;
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>n>>k;
for(int i=;i<n;i++)
{
int a,b;
cin>>a>>b;
s.pb(mp(a,-));
s.pb(mp(b,));
}
sort(s.begin(),s.end());
int tmp=;
for(int i=;i<s.size();i++)
{
if(s[i].second==-)
{
tmp++;
if(tmp==k)ans.pb(s[i].first);
}
else
{
if(tmp==k)ans.pb(s[i].first);
tmp--;
}
}
cout<<ans.size()/<<endl;
for(int i=;i<ans.size()/;i++)
{
cout<<ans[*i]<<" "<<ans[*i+]<<endl;
}
}

http://codeforces.com/contest/612/problem/D的更多相关文章

  1. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  2. [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环

    E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...

  3. http://codeforces.com/contest/555/problem/B

    比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...

  4. http://codeforces.com/contest/610/problem/D

    D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. http://codeforces.com/contest/536/problem/B

    B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. http://codeforces.com/contest/535/problem/C

    C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. http://codeforces.com/contest/838/problem/A

    A. Binary Blocks time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. http://codeforces.com/contest/402/problem/E

    E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...

  9. codeforces.com/contest/251/problem/C

    C. Number Transformation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. HDMI转EDP芯片NCS8803简介

    NCS8803 HDMI-to-eDP w/ Scaler Features --Embedded-DisplayPort (eDP) Output 1/2/4-lane eDP @ 1.62/2.7 ...

  2. 极化码的matlab仿真(2)——编码

    第二篇我们来介绍一下极化码的编码. 首先为了方便进行编码,我们需要进行数组的定义 signal = randi([0,1],1,ST); %信息位比特,随机二进制数 frozen = zeros(1, ...

  3. 读取Execl表 导入数据库

    不知不觉博客园园林都两年多了,我是今年毕业的应届生,最近公司项目需要改动,很多的数据需要导入,很多的实体类需要些.考虑到这些问题自己写了两个winform版的小工具,一个是读取Execl数据导入数据库 ...

  4. MySQL(六)之MySQL常用操作符

    前言 在前面的MySQL学习中,我们学习了MySQL的安装,管理以及配置,还有是它的DDL.今天给大家分享一下,MySQL的操作符和它的函数这部分. 千里之行始于足下,做什么事情都要脚踏实地的去做才能 ...

  5. [js高手之路]深入浅出webpack教程系列7-( babel-loader,css-loader,style-loader)的用法

    什么是loader呢,官方解释为文件的预处理器,通俗点说webpack在处理静态资源的时候,需要加载各种loader,比如,html文件,要用html-loader, css文件要用css-loade ...

  6. Google的SPDY协议成为HTTP 2.0的基础

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt384 据TNW援引 IFTF HTTP 工作组主席 Mark Notting ...

  7. 【小白成长撸】--多项式求圆周率PI

    /*程序的版权和版本声明部分: *Copyright(c) 2016,电子科技大学本科生 *All rights reserved. *文件名:多项式求PI *程序作用:计算圆周率PI *作者:Amo ...

  8. 201521123061 《Java程序设计》第七周学习总结

    201521123061 <Java程序设计>第七周学习总结 1. 本周学习总结 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 贴上源 ...

  9. 201521123054《Java程序设计》第8周学习总结

    1. 本周学习总结 2. 书面作业 List中指定元素的删除(题目4-1) 1.1 实验总结 每次删除时下标需要-1:原理如图 统计文字中的单词数量并按出现次数排序(题目5-3) 2.1 伪代码(简单 ...

  10. 201521123112《Java程序设计》第7周学习总结

    1. 本周学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 下面先贴出contains的源代码: public boolean cont ...