Aglomerație

Time limit: 0.1s Memory limit: 16MB Input: aglomeratie.in Output: aglomeratie.out

You work as a courier in Alba Iulia. The city is represented as a matrix with NN rows and MM columns. Initially, the congestion level in each intersection (cell) of the matrix is 00.

Today is City Day, and the city hall has approved LL street events. Each event partially blocks a rectangular area, defined by the top-left corner (r1,c1)(r_1, c_1) and the bottom-right corner (r2,c2)(r_2, c_2), adding a value XX to the congestion level of all cells in that area. If the areas overlap, the congestion level is cumulative.

The headquarters of the company where you pick up the packages is located at coordinates (is,js)(i_s, j_s). Your scooter has a maximum crowd navigation power equal to KK. This means you cannot enter a cell where the congestion level is strictly greater than KK.

Traversing from a valid cell to another valid adjacent cell (Up, Down, Left, Right) takes exactly 1 minute.

Task

You received a list of CC orders. For each order, you must leave the central headquarters (is,js)(i_s, j_s) and reach the client located at coordinates (x,y)(x, y). Calculate the minimum time (in minutes) needed to reach each of the CC clients. (Deliveries are independent, it is considered that for each order you always start from the headquarters).

Input data

The first line contains five integer numbers N,M,L,CN, M, L, C and KK.
The second line contains two integer numbers isi_s and jsj_s, representing the coordinates of the headquarters.
The next LL lines contain five integer numbers each: r1,c1,r2,c2r_1, c_1, r_2, c_2 and XX, representing the coordinates of the areas where events take place and the added congestion level.
The next CC lines contain two integer numbers each xx and yy, representing the coordinates where the clients are waiting.

Output data

You will print CC lines. The ii-th line will contain a single integer number, representing the minimum time needed to reach the ii-th client. If it is impossible to reach that client, it will print 1-1.

Constraints and clarifications

  • 1N,M10001 \leq N, M \leq 1\,000
  • 1L,C1000001 \leq L, C \leq 100\,000
  • 1X,K1091 \leq X, K \leq 10^9
  • It is guaranteed that the headquarters (is,js)(i_s, j_s) has a final congestion level K\leq K.
  • For 30%30\% of the score: N,M100N, M \leq 100 and L,C1000L, C \leq 1\,000

Example

aglomeratie.in

4 4 2 3 5
4 4
1 1 2 2 4
2 2 3 3 3
1 4
2 2
3 1

aglomeratie.out

3
-1
4

Explanation

The initial matrix has a dimension of 4×44 \times 4. The scooter's power limit is K=5K = 5. The headquarters is at (4,4)(4, 4).

After applying the 22 street events, the congestion matrix looks like this:

4 4 0 0
4 7 3 0
0 3 3 0
0 0 0 0

We observe that cell (2,2)(2, 2) has a congestion of 77, so it is completely forbidden because 7>57 > 5.

  • Client 1 is located at (1,4)(1, 4): The optimal path is (4,4)(3,4)(2,4)(1,4)(4,4) \rightarrow (3,4) \rightarrow (2,4) \rightarrow (1,4). Time = 33 minutes.
  • Client 2 is located at (2,2)(2, 2): It is located in a completely blocked intersection. Time = 1-1.
  • Client 3 is located at (3,1)(3, 1): The path bypasses the crowded center: (4,4)(4,3)(4,2)(4,1)(3,1)(4,4) \rightarrow (4,3) \rightarrow (4,2) \rightarrow (4,1) \rightarrow (3,1). Time = 44 minutes.

Log in or sign up to be able to send submissions!