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 ...
随机推荐
- windows下安装 postgresql
1. 下载PostgreSQL的源代码.解压. 2. 在Windows平台下编译需要跳过一个权限的检测,否则在编译的时候可能会出现错误. 在\src\backend\main\main.c文件中将 ...
- Arc071_F Infinite Sequence
传送门 题目大意 给定一个数$n$,构造一个无限长的序列$A$,使得 $\forall i,j\geq n,A_i=A_j$ $\forall i<j<k\leq i+a_i,A_j=A_ ...
- 【LeetCode】048. Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- [转]PNG8和PNG24的区别
首先我们要知道: 1.png8和png24的根本区别,不是颜色位的区别,而是存储方式不同. 2.png8有1位的布尔透明通道(要么完全透明,要么完全不透明),png24则有8位(256阶)的布尔透明通 ...
- Asp.net工作流workflow实战之书签(二)
1.winform(web程序)下使用工作流 怎样才能像控制台那样让winform或web页面窗体阻塞等待工作流的继续执行呢 2.BookMark书签 书签:和一般的书签看书的时候方便查看上次看的内容 ...
- Azure 用户自定义路由 (User Defined Route)
在公有云环境中,用户创建了一个Vnet,添加了若干个网段后,这几个网段是全联通的状态. 如果希望在Vnet中添加一些功能性的设备,比如防火墙.IPS.负载均衡设备等,就需要进行用户自定义路由的配置. ...
- mysql查询语句例题
1.一条SQL语句查询两表中两个字段 首先描述问题,student表中有字段startID,endID.garde表中的ID需要对应student表中的startID或者student表中的endID ...
- k8s 基础 核心概念
Pod pod是若干相关容器的集合,Pod包含的容器运行在同一台宿主机上,这些容器使用相同的网络命名空间,ip地址和端口,相互之间能通过localhost来发现和通信.另外,这些容器还可共享一块存储空 ...
- SQL 时间及字符串操作
都是一些很基础很常用的,在这里记录一下 获取年月日: year(时间) ---获取年,2014 month(时间) ----获取月,5 day(时间) -----获取天,6 如果月份或日期不足两位数, ...
- URL中#符号的作用
转自http://blog.sina.com.cn/s/blog_6f9eb2dd0100sk97.html 一.#的涵义 #代表网页中的一个位置.其右面的字符,就是该位置的标识符.比如, ...