Problem Introduction

The goal in this problem is given a set of segments on a line and a set of points on a line, to count, for each point, the number of segments which contain it.

Problem Description

Task.In this problem you are given a set of points on a line and a set of segments on a line. The goal is to compute, for each point, the number of segments that contain this point.

Input Format.The first line contains two non-negative integers \(s\) and \(p\) defining the number of segments and the number of points on a line, respectively. The next \(s\) lines contain two integers \(a_i, b_i\) defining the \(i\)-th segment \([a_i, b_i]\). The next line contains \(p\) integers defining points \(x_1,x_2,\cdots ,x_p\).

Constraints.\(1 \leq s,p \leq 50000;-10^8 \leq a_i \leq b_i \leq 10^8\) for all \(0 \leq i < s;-10^8 \leq x_j \leq 10^8\) for all \(0 \leq j < p\).

Output Format.Output \(p\) non-negative integers \(k_0,k_1,\cdots,k_{p-1}\) where \(k_i\) is the number of segments which contain \(x_i\). More formally,
\(k_i=|{j:a_j \leq x_i \leq b_j}|\)

Sample 1.
Input:

2 3
0 5
7 10
1 6 11

Output:

1 0 0

Sample 2.
Input:

1 3
-10 10
-100 100 0

Output:

0 0 1

Sample 3.
Input:

3 2
0 5
-3 2
7 10
1 6

Output:

2 0

Solution

# Uses python3
import sys
from itertools import chain

def fast_count_segments(starts, ends, points):
    cnt = [0] * len(points)
    a = zip(starts, [float('-inf')]*len(starts))
    b = zip(ends, [float('inf')]*len(ends))
    c = zip(points, range(len(points)))
    sortedlist = sorted(chain(a,b,c), key=lambda a : (a[0], a[1]))
    stack = []
    for i, j in sortedlist:
        if j == float('-inf'):
            stack.append(j)
        elif j == float('inf'):
            stack.pop()
        else:
            cnt[j] = len(stack)
    return cnt

def naive_count_segments(starts, ends, points):
    cnt = [0] * len(points)
    for i in range(len(points)):
        for j in range(len(starts)):
            if starts[j] <= points[i] <= ends[j]:
                cnt[i] += 1
    return cnt

if __name__ == '__main__':
    input = sys.stdin.read()
    data = list(map(int, input.split()))
    n = data[0]
    m = data[1]
    starts = data[2:2 * n + 2:2]
    ends   = data[3:2 * n + 2:2]
    points = data[2 * n + 2:]
    #use fast_count_segments
    cnt = fast_count_segments(starts, ends, points)
    for x in cnt:
        print(x, end=' ')

[UCSD白板题] Points and Segments的更多相关文章

  1. [UCSD白板题] Covering Segments by Points

    Problem Introduction You are given a set of segments on a line and your goal is to mark as few point ...

  2. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  3. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  4. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  5. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  6. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  7. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  8. [UCSD白板题] Sorting: 3-Way Partition

    Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...

  9. [UCSD白板题] Majority Element

    Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...

随机推荐

  1. SQL Server 2008R2数据库文件导入到SQL Server 2008数据库中

    最近,电脑重装系统之后,安装了SQL Server 2008.附加数据库文件的时候,发现无法附加,提示版本不对.想起来,原来的数据库版本是SQL Server 2008R2.低版本的数据库管理工具无法 ...

  2. Java线程的5个使用技巧

    萝卜白菜各有所爱.像小编我就喜欢Java.学无止境,这也是我喜欢它的一个原因.日常工作中你所用到的工具,通常都有些你从来没有了解过的东西,比方说某个方法或者是一些有趣的用法.比如说线程.没错,就是线程 ...

  3. nignx软件安装与调试

    1.通过yum或下载相应软件包安装nginx所需要的辅助软件:pcre.pcre-devel.openssl.openssl-devel.make.gcc.gcc+ 2.解压已经下载好的nginx软件 ...

  4. 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  5. springmvc 学习笔记_1

    一.回顾JavaWeb中的MVC设计模式 1)MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域 2)今天说的MVC特指一种表现层设计模式,不限于Java语言 二.回顾struts2+ ...

  6. python学习笔记-多进程

    multiprocessing from multiprocessing import Process import time def f(name): time.sleep(2) print('he ...

  7. WNDR3700V4恢复原厂固件(使用TFTP刷网件原厂固件)

    WNDR3700v4原厂固件下载地址: http://support.netgear.cn/doucument/More.asp?id=2203 操作方法: 1.将设备断电: 2.按住设备背面的Res ...

  8. aspnet超级链接 传递 当前页面 textbox值

    一共有两个窗体. 第一个窗体A传参到窗体B  A的参数是A窗体的textbox 窗体A代码 <html xmlns="http://www.w3.org/1999/xhtml" ...

  9. Dreamweaver扩展注意事项

    对Dreamweaver扩展做了一些整理. 扩展开发扩展(Extension),是应用程序给用户预留的二次开发接口.Dreamweaver提供了对菜单,插入栏(Insertbar),浮动框等GUI部件 ...

  10. javascript总结

    javascript:它是一种script脚本语言           脚本语言:就是可以和HTML混合在一起使用的语言,可以用来在IE的客                    户端进行程序编制,从 ...