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. 对dijkstra算法的自我理解,c#例子

    dijkstra该算法主要应用在求解最短路径,从最近点开始,广度搜索. 假设有向图中有10个顶点,求其中某个顶点a到其它顶点的最短路径..满足贪心算法的2个标准.时间复杂度为O(N2) 此问题可以进行 ...

  2. 书单.md

    0823 John Hoskin, An Ilustrated History of Thailand.Asia Books Co., Ltd.2015 0729 Gerald Graff, Cath ...

  3. strcpy(),strcat()的用法

    strcpy(): 定义一个字符串char a[20],和一个字符串c[]="i am a teacher!"; 把c复制到a中就可以这样用:strcpy(a,c); 这个函数包含 ...

  4. 初步认识html以及表格的制作

    12.21,冬至,天空中还下着小雨,雾霾也没有散去,但是也没有冲散节日的气氛,心情也是倍儿好. 今天学习了不少的内容,对我来说是对之前所学的一个巩固,内容比较多也比较杂乱一些,下面以例子的形式来表现: ...

  5. js之字符串操作

    1.字符串查找 var str = "source_content10"; if(str.indexOf("source_content") !== false ...

  6. Splinter学习--初探1,模拟百度搜索

    Splinter是以Selenium, PhantomJS 和 zope.testbrowser为基础构建的web自动化测试工具,基本原理同selenium 支持的浏览器包括:Chrome, Fire ...

  7. JavaWeb 学习0010-今日问题 2016-12-3

    2016-12-3 1. 今天要做的第一个问题就是,怎么把网页变得好看点: addStudent/listStudent页面都有改动 其中 list页面还有了<c:if 的语句这是还没练习过得知 ...

  8. UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等

    (1)可以根据需要设置文本框的样式(包括形状.边框颜色.背景等). (2)可以根据需要设置文字显示样式(包括输入密码时的密文显示.文字横向居中.纵向居中上下.输入的文字是否首席木大写.文字超过后是否缩 ...

  9. 微信web开发者工具初探

    最近需要在微信企业号中挂接网页,之前也没有接触过微信开发,刚开始也不知道怎么调试,后来同事介绍使用“微信web开发者工具”,于是在网上下了一个,使用了一下的确很好用.它不仅支持Android和IOS同 ...

  10. 日期函数(sql)

    SQL 标量函数----->日期函数 day() .month().year().2009年02月23日 星期一 11:30 SQL 标量函数----->日期函数 day() .month ...