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 ...
随机推荐
- org.apache.catalina.core.StandardWrapperValve invoke报错
tomcat报错如下: HTTP Status 404 - Servlet xxx is not available type Status report message Servlet xxx is ...
- Struts2 - ModelDriven 拦截器、Preparable 拦截器
开篇:拦截器在Struts中的作用 在我们的web.xml中,我们配置了一个过滤器,实现将所有请求交付StrutsPrepareAndExecuteFilter类.一旦接受到任意action的请求,该 ...
- uoj problem 31 猪猪侠再战括号序列
题目大意: 给定一个长度为2n的括号序列.定义一个关于区间[l,r]的翻转操作为位置平移对调. 即翻转")))()("可以得到"()()))((" 用不超过n次 ...
- bzoj 1242 弦图判定 MCS
题目大意: 给定一张无向图,判断是不是弦图. 题解: 今天刚学了<弦图与区间图> 本来写了一个60行+的学习笔记 结果因为忘了保存重启电脑后被还原了... 那就算了吧. MCS最大势算法, ...
- docker-建立私有registry
我们知道可以使用hub.docker.com作为我们公共或者私有的registry.但由于服务器在国外的原因,网速会非常的慢.所以我们在利用docker开发构建容器服务时,我们希望能够建立自己的私有r ...
- WPF如何更改系统控件的默认高亮颜色 (Highlight brush)
我们在用WPF时, 经常会对系统控件的默认高亮等等颜色进行更改. 以前通常是用controlTemplate来实现. 今天发现一个更合理或者简单的方法: 用系统默认颜色的key, 比如 SystemC ...
- spring IOC 注解@Resource
1.@Resource(重要)a)加入 :j2ee/common-annotations.jar b)默认按名称,名称找不到,按类型 默认按照名称setName1到xml中找和id相同的,没有的话再找 ...
- Poj1050_To the Max(二维数组最大字段和)
一.Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is an ...
- UVa11624(逃离火焰问题)
#include"cstdio" #include"queue" #include"cstring" using namespace std ...
- virtual judge(专题一 简单搜索 B)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...