-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoints_and_segments.py
32 lines (29 loc) · 1014 Bytes
/
points_and_segments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sys
from itertools import chain
def fast_count_segments(starts, ends, points):
cnt = [0] * len(points)
start_points = zip(starts, ['l'] * len(starts), range(len(starts)))
end_points = zip(ends, ['r'] * len(ends), range(len(ends)))
point_points = zip(points, ['p'] * len(points), range(len(points)))
sort_list = chain(start_points, end_points, point_points)
sort_list = sorted(sort_list, key=lambda a: (a[0], a[1]))
segment_count = 0
for num, letter, index in sort_list:
if letter == 'l':
segment_count += 1
elif letter == 'r':
segment_count -= 1
else:
cnt[index] = segment_count
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:]
cnt = fast_count_segments(starts, ends, points)
for x in cnt:
print(x, end=' ')