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 ...
随机推荐
- Java进阶07 嵌套类
到现在为止,我们都是在Java文件中直接定义类.这样的类出现在包(package)的级别上.Java允许类的嵌套定义. 这里将讲解如何在一个类中嵌套定义另一个类. 嵌套 内部类 Java允许我们在类的 ...
- Maven 将jar导入本地maven仓库
目录 环境变量配置maven 执行一下命令即可 诚邀访问我的个人博客:我在马路边 更好的阅读体验点击查看原文:Maven将jar倒入本地maven仓库 原创博客,转载请注明出处 @ 在Java项目开发 ...
- hdu 5269 ZYB loves Xor I 分治 || Trie
题目大意: 长度为\(n\)的数组A.求对于所有数对\((i,j)(i \in [1,n],j \in [1,n])\),\(lowbit(A_i xor A_j)\)之和.答案对998244353取 ...
- POJ1365:素数
Prime Land Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3552 Accepted: 1609 Descri ...
- boost::io_service解读
boost::io_service解读 asio是boost提供的一个c++异步编程模型库,其核心类io_service,在多线程编程里面提供了任务队列和任务分发功能,在socket.io编程里主要作 ...
- 【转】 Pro Android学习笔记(五七):Preferences(1):ListPreference
目录(?)[-] 例子1ListPreference小例子 定义一个preferences XML文件 继承PreferenceActivity 用户定制偏好的读取 第一次运行时设置缺省值 设置Cat ...
- 完美前向保密PFS
===========来自网友=========== “前向安全性”应当是叫做“forward security”.该定义最早是由Mihir Bellare和Sara K. Miner在 CRYPTO ...
- pycharm安装 package报错:module 'pip' has no attribute 'main'
转自: <pycharm安装 package报错:module 'pip' has no attribute 'main'> https://www.cnblogs.com/Fordest ...
- asp页面重定向
asp页面重定向 1.当你点击某页面时(没有登录),而此页面需要登录,登录后页面需要定向到你之前操作的页面时 就用到了重定向. 2.login.aspx?redirUrl="重定向的页面地址 ...
- Jumony.Core非常厉害的一个开源项目!
简单的说,就是解析html文档的,以前发送一个get请求获取一个页面的html文本后,想要获取里面的数据都是使用正则表达式.(非常的苦逼), 现在用这个获取就very easy! 安装的话在Nu Ge ...