JOI Spring Training Camp 2013/14
Original Statement / Submission Site
JOI-kun is a university student who commutes to university by bus. Both his home and his university are located in IOI City.
There are N bus stops in IOI City, numbered from 1 to N. The bus stop nearest to JOI-kun's home is bus stop 1, and the bus stop nearest to his university is bus stop N.
There are M buses operating in IOI City. Each bus runs exactly once per day: it departs from a specified bus stop at a specified time and arrives at another specified bus stop at a specified time. No bus operates across midnight. JOI-kun cannot board or leave a bus at an intermediate stop.
Every day, JOI-kun travels to university using one or more buses. The time required to transfer between buses can be ignored. In other words, to board a bus departing from a bus stop at a certain time, it is sufficient that JOI-kun arrives at that bus stop at or before the bus's departure time. He may also use the same bus stop more than once.
Under these conditions, JOI-kun wants to know how late he may leave home and still arrive at university in time for class. The starting time of the first class differs from day to day.
For each of Q days, the latest time by which he must arrive at bus stop N is given. For each day, determine the latest time by which he must arrive at bus stop 1 in order to reach university in time.
Given information about the bus services and, for each of Q days, the deadline for arriving at bus stop N, determine the latest time by which JOI-kun must arrive at bus stop 1 on each day.
The input is given from standard input in the following format:
N M A1 B1 X1 Y1 A2 B2 X2 Y2 ... AM BM XM YM Q L1 L2 ... LQ
N and M, representing the number of bus stops and the number of buses.M lines contains four integers Ai, Bi, Xi, and Yi. The i-th bus departs from bus stop Ai at time Xi and arrives at bus stop Bi at time Yi.Q.Q lines contains an integer Lj, the latest time by which JOI-kun must arrive at bus stop N on the j-th day.Print Q lines.
On the j-th line, print the latest time by which JOI-kun must arrive at bus stop 1 on the j-th day. If it is impossible to reach bus stop N by time Lj, print -1.
2 ≤ N ≤ 1000001 ≤ M ≤ 3000001 ≤ Ai, Bi ≤ NAi ≠ Bi0 ≤ Xi < Yi < 864000001 ≤ Q ≤ 1000000 ≤ Lj < 86400000N ≤ 2000M ≤ 2000Q = 1N ≤ 2000M ≤ 2000Q = 1There are no additional constraints.
5 6 1 2 10 25 1 2 12 30 2 5 26 50 1 5 5 20 1 4 30 40 4 5 50 70 4 10 30 60 100
-1 5 10 30
It is impossible to arrive at bus stop 5 by time 10.
To arrive by time 30, JOI-kun can board the fourth bus at time 5.
To arrive by time 60, he can proceed as follows:
10.2 at time 25, wait for 1 millisecond, and board the third bus.5 at time 50.To arrive by time 100, he can proceed as follows:
30.4 at time 40, wait for 10 milliseconds, and board the sixth bus.5 at time 70.3 8 1 2 1 5 1 3 0 1 1 3 2 8 2 3 2 3 2 3 3 4 2 3 4 5 2 3 5 6 2 3 6 7 6 3 4 5 6 7 8
0 0 0 1 1 2