zoukankan      html  css  js  c++  java
  • Codeforces Round #501 (Div. 3) 1015A Points in Segments (前缀和)

    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 (1lirim1≤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 lxrl≤x≤r .

    Input

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

    The next nn lines contain two integers each lili and riri (1lirim1≤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代码(原题解):

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main() {
     6 #ifdef _DEBUG
     7 //    freopen("input.txt", "r", stdin);
     8 //    freopen("output.txt", "w", stdout);
     9 #endif
    10     
    11     int n, m;
    12     cin >> n >> m;
    13     vector<int> cnt(m + 2);
    14     for (int i = 0; i < n; ++i) {
    15         int l, r;
    16         cin >> l >> r;
    17         ++cnt[l];
    18         --cnt[r + 1];
    19     }
    20     for (int i = 1; i <= m; ++i)
    21         cnt[i] += cnt[i - 1];
    22     vector<int> ans;
    23     for (int i = 1; i <= m; ++i) {
    24         if (cnt[i] == 0)
    25             ans.push_back(i);
    26     }
    27     
    28     cout << ans.size() << endl;
    29     for (auto it : ans) cout << it << " ";
    30     cout << endl;
    31     
    32     return 0;
    33 }
  • 相关阅读:
    算法:基于分布的排序算法
    剑指offer:镜像二叉树
    算法:基于比较的排序算法
    LeetCode做题笔记-135
    初识YOLO
    PHP课设图览
    浅谈C语言整型与浮点型转换
    SQL Server EXPRESS 安装
    2020CCPC 东北四省(区域)赛题目一览
    2020CCPC 黑龙江省赛题目一览
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9572940.html
Copyright © 2011-2022 走看看