A. Points in Segments
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a set of nn segments on the axis OxOx , each segment has integer endpoints between 11 and mm inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers lili and riri (1≤li≤ri≤m1≤li≤ri≤m ) — coordinates of the left and of the right endpoints.

Consider all integer points between 11 and mm inclusive. Your task is to print all such points that don't belong to any segment. The point xx belongs to the segment [l;r][l;r] if and only if l≤x≤rl≤x≤r .

Input

The first line of the input contains two integers nn and mm (1≤n,m≤1001≤n,m≤100 ) — the number of segments and the upper bound for coordinates.

The next nn lines contain two integers each lili and riri (1≤li≤ri≤m1≤li≤ri≤m ) — the endpoints of the ii -th segment. Segments may intersect, overlap or even coincide with each other. Note, it is possible that li=rili=ri , i.e. a segment can degenerate to a point.

Output

In the first line print one integer kk — the number of points that don't belong to any segment.

In the second line print exactly kk integers in any order — the points that don't belong to any segment. All points you print should be distinct.

If there are no such points at all, print a single integer 00 in the first line and either leave the second line empty or do not print it at all.

Examples
Input

Copy
3 5
2 2
1 2
5 5
Output

Copy
2
3 4
Input

Copy
1 7
1 7
Output

Copy
0
Note

In the first example the point 11 belongs to the second segment, the point 22 belongs to the first and the second segments and the point 55 belongs to the third segment. The points 33 and 44 do not belong to any segment.

In the second example all the points from 11 to 77 belong to the first segment.

题目大意:n个线段 [l, r], 然后1~m个点,输出哪些点不属于任何线段。

当然是个水题,时间复杂度O(n*m), 如果  n,m <= 10^7呢?

可以用前缀和O(n+m)解答:

给出  l,r  暴力想法直接 vis[l]~vis[r] 标记 1,但是有更好的想法。

将 vis[l]++,  vis[r+1]-- ,求前缀和的时候,虽然中间的没有标记1,但是前缀和往后延申就达到标记效果了。将vis[r+1]--, 传到r+1的时候就(-1) + 1 = 0,  也就是没有出现过了。

例如 :  点1  2  3 4 5

给出线段 【2 4】, 【4,5】;可以看出答案为   1

点              0    1    2     3      4      5       6

[2,4]           0     0    1     0     0      -1      0

[4,5]           0     0    1     0     1       0      -1

前缀和       0     0    1     1     2       2       1(只有点1为0)

AC代码(原题解):

 #include <bits/stdc++.h>

 using namespace std;

 int main() {
#ifdef _DEBUG
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif int n, m;
cin >> n >> m;
vector<int> cnt(m + );
for (int i = ; i < n; ++i) {
int l, r;
cin >> l >> r;
++cnt[l];
--cnt[r + ];
}
for (int i = ; i <= m; ++i)
cnt[i] += cnt[i - ];
vector<int> ans;
for (int i = ; i <= m; ++i) {
if (cnt[i] == )
ans.push_back(i);
} cout << ans.size() << endl;
for (auto it : ans) cout << it << " ";
cout << endl; return ;
}

Codeforces Round #501 (Div. 3) 1015A Points in Segments (前缀和)的更多相关文章

  1. Codeforces Round #245 (Div. 2) A. Points and Segments (easy) 贪心

    A. Points and Segments (easy) Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...

  2. Codeforces Round #245 (Div. 2) A - Points and Segments (easy)

    水到家了 #include <iostream> #include <vector> #include <algorithm> using namespace st ...

  3. Codeforces Round #486 (Div. 3) D. Points and Powers of Two

    Codeforces Round #486 (Div. 3) D. Points and Powers of Two 题目连接: http://codeforces.com/group/T0ITBvo ...

  4. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  5. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  6. Codeforces Round #501 (Div. 3)

    A - Points in Segments 题意:implement #include<bits/stdc++.h> using namespace std; typedef long ...

  7. Codeforces Round #466 (Div. 2) -A. Points on the line

    2018-02-25 http://codeforces.com/contest/940/problem/A A. Points on the line time limit per test 1 s ...

  8. Codeforces Round #319 (Div. 1) C. Points on Plane 分块

    C. Points on Plane Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/pro ...

  9. Codeforces Round #466 (Div. 2) A. Points on the line[数轴上有n个点,问最少去掉多少个点才能使剩下的点的最大距离为不超过k。]

    A. Points on the line time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. c++的静态变量与静态函数

    参考文献:静态成员函数和静态成员 一.静态变量: 1.静态变量属于类的变量,为类共享,在编译期间就分配好了内存. 2.静态变量在头文件中声明(和全局变量一样不要在头文件中定义静态变量),在.cpp文件 ...

  2. 第二次C语言实验报告

    #一.设计题目,设计思路,实现方法 ##设计题目 15-10 找最长的字符串,14-5 指定位置输出字符串,13-6 数组循环右移,12-5 查找指定字符,11-5 打印杨辉三角. ##设计思路 15 ...

  3. 我的SIP开发之路

    http://hi.baidu.com/ltlovelty/blog/item/837baf1ece7fc6f11ad57647.html 经过对SIP协议和开源协议栈快半年的研究,我现在终于有点入门 ...

  4. Java中“分号”引起的陷阱

    对于这类问题而言,难度不大,多半是由于我们有误操作引起的.但在查找问题所在的时候我们有可能需要花费一番功夫了. 实例一: package com.yonyou.test; /** * 测试类 * @a ...

  5. 关于导入excel报错的处理(xls,xlsx)

    关于导入excel报错的处理(xls,xlsx) 最近在做一个将excel导入到dataGriview中的小功能在做的过程中遇到以下问题: 链接excel的链接串是这样写的 string strCon ...

  6. LaTeX技巧203:如何实现等号对齐_LaTeX_Fun_新浪博客

    LaTeX技巧203:如何实现等号对齐_LaTeX_Fun_新浪博客 我们在进行公式的输入排版的时候,通常希望公式比较齐整,所以需要一些等号对齐,或者左对齐,关于公式的左对齐前文已经介绍了方法.htt ...

  7. java 终端输入小结,输入到数组、文件等(持续更新)

    一:将键盘输入的数存到数组中,数组长度未知 public class Test{ public static void main(String[] args){ Scanner sc = new Sc ...

  8. VS 关于无法打开项目文件,此安装不支持该项目类型的问题

    用VS打开后有时会出现类似: 无法打开项目文件,此安装不支持该项目类型 的错误,这个错误一般都是由于用低版本VS打开高版本项目文件造成的 其中包括: 1.用VS2003 打开包括VS2005以上版本项 ...

  9. JQuery 1.6之后,获取属性推荐用prop

    今天在做界面,要获取CheckBox的是否被选中, var ck=$("#id").attr("checked"); 但是这样取得的值是undefined, 查 ...

  10. PHP和MySql数据库,如何获取每个分类的记录的总数

    示例的数据库,如下: 本文说的问题,就是统计每个学院(Sdept)的人数. 还有很多情况,比如说:在制作CMS的时候,文章有个分类问题,所有的文章的记录都是存放到同一个表中. 当我们需要统计每个分类的 ...