[UCSD白板题] Points and Segments
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的更多相关文章
- [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 ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [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 ...
- [UCSD白板题] Sorting: 3-Way Partition
Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...
- [UCSD白板题] Majority Element
Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...
随机推荐
- 【C#】项目优化实战
[C#]项目优化实战 一. 数据库设计 1. 常量的枚举值直接存中文不要存数字(注意是常量,如果显示值可变就不能) 例如:男女,在数据库中不要存1和0,直接存男和女. 这样的好处:读取数据的时候可以避 ...
- HTML5 ---localStorage
HTML5中提供了localStorage对象可以将数据长期保存在客户端,直到人为清除. localStorage提供了几个方法: 1.存储:localStorage.setItem(key,valu ...
- SQL Server创建复合索引时,复合索引列顺序对查询的性能影响
说说复合索引 写索引的博客太多了,一直不想动手写,有一下两个原因:一是觉得有炒剩饭的嫌疑,有兄弟曾说:索引吗,只要在查询条件上建索引就行了,真的可以这么暴力吗?二来觉得,索引是个非常大的话题,很难概括 ...
- 如何理解反向传播 Backpropagation 梯度下降算法要点
http://colah.github.io/posts/2015-08-Backprop/ http://www.zhihu.com/question/27239198 待翻译 http://blo ...
- python学习之——splinter使用
开始学习使用splinter工具了,目前是摸索中,先熟悉splinter工具的使用方法~~ 实现功能: 打开firefox浏览器->www.baidu.com->输入关键词 python, ...
- XtraGrid RepositoryItemCheckEdit 显示状态以及单选多选问题
RepositoryItemCheckEdit默认有三种状态,选中状态.未选中状态和半选中状态(半选中状态通常用在TreeList中如果父节点下的子节点有选中的有未选中的,则父节点状态为半选中状态). ...
- ueditor不自动加P解决方法
百度的Ueditor编辑器出于安全考虑; 用户在html模式下粘贴进去的html文档会自动被去除样式和转义. 虽然安全的,但是非常不方便. 做一下修改把这个功能去掉. 一.打开ueditor.all. ...
- SQL Server Management Studio 2012 设置脚本默认保存路径
特别说明,本文是从这里 修改SQL Server Management Studio默认设置提高开发效率. "抄过来的",为方便个人记忆才写此文(非常感谢这哥们儿的分享.) 原文地 ...
- soapUI 时间格式
用soapUI测试webservice,接收DateTime格式,总是包 not a valid AllXsd value 老外说,必须用ISO8601格式,如: 2009-03-13T22:16:0 ...
- ng-repeat && ng-options的故事
ng-repeat && ng-options的故事 1. <select class="input-small" ng-model="newH ...