#P14678. [ICPC 2025 Seoul R] Segments

[ICPC 2025 Seoul R] Segments

题目描述

In the first quadrant of a coordinate plane, you are given nn line segments parallel to the xx-axis. Each segment SiS_i (1in1 \le i \le n) is represented by the coordinates of its left and right endpoints, (li,yi)(l_i, y_i) and (ri,yi)(r_i, y_i), respectively. All coordinates are positive integers.

You must now answer qq queries. For each query, a vertical line x=px = p, parallel to the yy-axis, is given. The vertical line is represented by a single positive integer pp.

If each segment SiS_i is horizontally extended, it will eventually meet the line x=px = p at the point (p,yi)(p, y_i). If the segment, including its endpoints, already meets x=px = p, no extension is needed. For example, suppose there are 5 segments {(2,3),(5,3)}\{(2,3), (5,3)\}, {(4,6),(9,6)}\{(4,6), (9,6)\}, {(8,2),(12,2)}\{(8,2), (12,2)\}, {(11,4),(13,4)}\{(11,4), (13,4)\}, and {(14,5),(17,5)}\{(14,5), (17,5)\}, and a single line x=11x = 11. The first segment must be extended by 6 to the right, the second segment 2 to the right, the third and the fourth segments 0, and the fifth segment 3 to the left for each to meet x=11x = 11.

For each query, determine the maximum among the extension lengths required for all segments to meet the line x=px = p. Formally, let dist(p,Si)\text{dist}(p, S_i) denote the distance that segment SiS_i must be extended to intersect x=px = p at (p,yi)(p, y_i). For each query, output max1indist(p,Si)\max_{1 \le i \le n} \text{dist}(p, S_i). In the example above, the answer to the query is 6. See the figure below.

:::align{center} :::

Given nn segments and qq queries, write a program to output the maximum extension length for each query.

输入格式

Your program is to read from standard input. The input starts with a line containing two integers nn (1n2×1061 \le n \le 2 \times 10^6) and qq (1q2×1061 \le q \le 2 \times 10^6), where nn is the number of line segments and qq is the number of queries. In the following nn lines, the ii-th line contains three integers, lil_i, rir_i, and yiy_i (1liri1091 \le l_i \le r_i \le 10^9, 1yi1031 \le y_i \le 10^3), where lil_i (resp. rir_i) is the xx-coordinate of left (resp. right) endpoint of SiS_i and yiy_i is the yy-coordinate of both endpoints of SiS_i. In the following qq lines of queries, the jj-th line contains one integer pjp_j (1pj1091 \le p_j \le 10^9) which denotes the vertical line x=pjx = p_j.

输出格式

Your program is to write to standard output. Print exactly one line per each query. The jj-th line should contain the maximum among the extension lengths required for all segments to meet x=pjx = p_j at (pj,yj)(p_j, y_j).

5 3
2 5 3
4 9 6
8 12 2
11 13 4
14 17 5
11
5
1
6
9
13
4 8
1 4 7
3 7 5
10 13 8
12 15 2
13
7
4
8
3
11
1
16
9
5
8
4
9
7
11
12