#P10772. [NOISG 2021 Qualification] Departure

[NOISG 2021 Qualification] Departure

Problem Description

There is a city with NN routes. On each route, every day at midnight, a bus departs from the starting point and arrives at the destination at midnight of the next day. The start of the ii-th bus is SiS_i, and the end is EiE_i. People can board, get off, or transfer at any position along a bus route.

The stadium is the center of this city, with coordinate 00. A point xx kilometers to the west of the stadium has coordinate x-x, and a point xx kilometers to the east has coordinate xx.

Now there are MM people who need to go home from the stadium. You need to find the shortest time for each person to get home.

Input Format

The first line contains two integers N,MN, M.

Lines 22 to N+1N + 1 each contain two integers Si,EiS_i, E_i.

Line N+2N + 2 contains MM integers PjP_j, representing each person's home.

Output Format

Output MM lines. Each line contains two integers a,ba, b (gcd(a,b)=1\gcd(a,b)=1), meaning that this person can get home at the earliest after ab\dfrac{a}{b} days.

5 8
0 5
3 11
1 -8
4 10
1 -3
1 2 5 6 8 -3 -7 11
1 5
2 5
1 1
4 3
13 8
4 9
8 9
2 1
3 2
-1 4
-2 5
0 -5
4 -4
6 7
4 5
5 3
0 2
2 4
4 6
6 8
8 10
9 10 10
9 2
5 1
5 1

Hint

Constraints

This problem uses bundled testdata.

Define $\operatorname{sign}(x) = \begin{cases} 1 &\text{if } x > 0 \\ 0 &\text{if } x = 0 \\ -1 &\text{if } x < 0 \\ \end{cases}$.

Subtask 0 is the sample.

Subtask 1 (10 pts): N104N \leq 10^4, M103M \leq 10^3, $\operatorname{sign}(S_i)\neq\operatorname{sign}(E_i)$.

Subtask 2 (14 pts): N100N \leq 100, M103M \leq 10^3.

Subtask 3 (12 pts): N103N \leq 10^3, M105M \leq 10^5, it is guaranteed that the final answer values are all no more than 10310^3, and any coordinate lies on no more than 10210^2 routes.

Subtask 4 (8 pts): M103M \leq 10^3, it is guaranteed that any coordinate lies on no more than 10410^4 routes.

Subtask 5 (15 pts): M104M \leq 10^4, it is guaranteed that the final answer values are all no more than 10210^2.

Subtask 6 (13 pts): $\operatorname{sign}(S_i)\neq\operatorname{sign}(E_i)$.

Subtask 7 (28 pts): no special constraints.

It is guaranteed that 1N,M1061 \leq N, M \leq 10^6, 106Si,Ei,Pj106-10^6 \leq S_i,E_i,P_j \leq 10^6, SiEiS_i \neq E_i, and Pj0P_j \neq 0.

Translated by ChatGPT 5