Codeforces Round #501 (Div. 3) 1015A Points in Segments (前缀和)
1 second
256 megabytes
standard input
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 .
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.
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.
3 5
2 2
1 2
5 5
2
3 4
1 7
1 7
0
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 (前缀和)的更多相关文章
- 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 ...
- Codeforces Round #245 (Div. 2) A - Points and Segments (easy)
水到家了 #include <iostream> #include <vector> #include <algorithm> using namespace st ...
- 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 ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- 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 ...
- Codeforces Round #501 (Div. 3)
A - Points in Segments 题意:implement #include<bits/stdc++.h> using namespace std; typedef long ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- codeforces 622A A. Infinite Sequence (二分)
A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- FFmpeg 基本用法--此文转载,作为笔记
v 容器(Container) v 容器就是一种文件(封装)格式,比如flv.mkv.ts.mp4.rmvb.avi等.包含下面5种流以及文件头信息. v 流(Stream) v 是一种视频数 ...
- bzoj 2631: tree link-cut-tree
题目: Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u ...
- node好用的东东
supervisor 可参考: http://www.cnblogs.com/pigtail/archive/2013/01/08/2851056.html http://www.cnblogs.co ...
- 更改Linux时区的两种方法
在Azure上的Linux虚拟机启动后默认是UTC的时区.对很多应用要记录时间戳非常的不方便. 本文将介绍两种更改Linux时间戳的方法,供大家参考. 1.修改/etc/localtime文件 控制系 ...
- JVM插庄之一:JVM字节码增强技术介绍及入门示例
字节码增强技术:AOP技术其实就是字节码增强技术,JVM提供的动态代理追根究底也是字节码增强技术. 目的:在Java字节码生成之后,对其进行修改,增强其功能,这种方式相当于对应用程序的二进制文件进行修 ...
- stm32 奇怪的位赋值问题 出错了
转载请注明出处:http://blog.csdn.net/qq_26093511/article/category/6094215 1.在51单片机里 ,下面这两种操作方法都是一样的,没有什么问题! ...
- hibernate HQL查询
hql(都要在事务中完成)session.beginTransaction();session.getTransaction().commit(); session.beginTransaction( ...
- linux命令-gzip压缩
把很大的目录/文件压缩成更小的文件,传输节省带宽,从服务端到客户端下载过程节省时间,减少带宽,节省使用率.使用cpu的资源. 压缩命令gzip [root@wangshaojun ~]# ls111. ...
- shell入门-tr替换字符和split切割大文件
命令:tr 说明:替换字符 格式tr ‘原字符’ ‘新字符’ 可以是范围字符,指定字符 命令:split 选项:-b 50m 1.txt 根据大小分割 单位是b不用单位,单位是兆加m -l 100 ...